/** * UPDATE /api/weights/{weights} * @param Request $request * @param Weight $weight * @return Response */ public function update(Request $request, Weight $weight) { // Create an array with the new fields merged $data = array_compare($weight->toArray(), $request->only(['weight'])); $weight->update($data); $weight = $this->transform($this->createItem($weight, new WeightTransformer()))['data']; return response($weight, Response::HTTP_OK); }
public function run() { Eloquent::unguard(); Weight::truncate(); $users = User::all(); $faker = Faker::create(); foreach ($users as $user) { //Create a weight entry for the last 5 days foreach (range(0, 4) as $index) { Weight::create(['date' => Carbon::today()->subDays($index)->format('Y-m-d'), 'weight' => $index / 10 + 50, 'user_id' => $user->id]); } } }
/** * @test * @return void */ public function it_can_update_a_weight_entry() { DB::beginTransaction(); $this->logInUser(); $weight = Weight::forCurrentUser()->first(); $response = $this->call('PUT', '/api/weights/' . $weight->id, ['weight' => 3.8]); $content = json_decode($response->getContent(), true); // dd($content); $this->checkWeightKeysExist($content); $this->assertEquals(3.8, $content['weight']); $this->assertEquals(200, $response->getStatusCode()); DB::rollBack(); }
/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { parent::boot($router); Route::bind('exercises', function ($id) { return Exercise::forCurrentUser()->findOrFail($id); }); Route::bind('exerciseEntries', function ($id) { return ExerciseEntry::forCurrentUser()->findOrFail($id); }); Route::bind('menuEntries', function ($id) { return MenuEntry::forCurrentUser()->findOrFail($id); }); Route::bind('exerciseSeries', function ($id) { return ExerciseSeries::forCurrentUser()->findOrFail($id); }); Route::bind('foods', function ($id) { return Food::forCurrentUser()->findOrFail($id); }); Route::bind('recipes', function ($id) { return Recipe::forCurrentUser()->findOrFail($id); }); Route::bind('exerciseTags', function ($id) { return Tag::forCurrentUser()->where('for', 'exercise')->findOrFail($id); }); Route::bind('recipeTags', function ($id) { return Tag::forCurrentUser()->where('for', 'recipe')->findOrFail($id); }); Route::bind('foodUnits', function ($id) { return Unit::forCurrentUser()->where('for', 'food')->findOrFail($id); }); Route::bind('exerciseUnits', function ($id) { return Unit::forCurrentUser()->where('for', 'exercise')->findOrFail($id); }); Route::bind('timers', function ($id) { return Timer::forCurrentUser()->findOrFail($id); }); Route::bind('activities', function ($id) { return Activity::forCurrentUser()->findOrFail($id); }); Route::bind('weights', function ($idOrDate) { if (strrpos($idOrDate, '-')) { //parameter is the date of the entry $weight = Weight::forCurrentUser()->where('date', $idOrDate)->first(); } else { //parameter is the id of the entry $weight = Weight::forCurrentUser()->findOrFail($idOrDate); } return $weight; }); /** * $parameter is either the id or the date */ Route::bind('journal', function ($parameter) { /** * @VP: * Is there a better way to check if the $parameter is an * id or a date? When I tried using Carbon to create an object from * the parameter, it threw an exception when the $parameter was the id, * whereas I just wanted a boolean. */ if (strrpos($parameter, '-')) { //$parameter is the date of the entry $journal = Journal::forCurrentUser()->where('date', $parameter)->first(); } else { //$parameter is the id of the entry $journal = Journal::forCurrentUser()->findOrFail($parameter); } return $journal; }); }