September 2017
Intermediate to advanced
244 pages
6h 44m
English
It will be served by the update() method. Again, it is based on ID provided, so we need to check if that ID is valid or not. So here is what we will do:
public function update(Request $request, $id){ $input = $request->all(); $post = $this->post->find($id); if(!$post) { abort(404); } $post->fill($input); $post->save(); return $post;}
Here, we have used model's fill() method, which will assign Post model with fields and values in $input and will then save it with the save() method. In Laravel documentation, you can see Insert and Update using Eloquent in different ways, which can be handy in different places: https://laravel.com/docs/5.4/eloquent#inserting-and-updating-models.
Read now
Unlock full access