Laravel 5.8 From Scratch: Config, ENV, Migrations, and Todos CRUD

: $todo->body }}</textarea> @if($errors->has('body')) {{– <-check if we have a validation error –}} <span class="invalid-feedback"> {{$errors->first('body')}} {{– <- Display the First validation error –}} </span> @endif </div> <button type="submit" class="btn btn-primary">Update</button> </form>@endsectionThis view is the same as the create.


php, the difference is that it’s submitting to update method of TodosController having a @method(‘PUT’) inside the form.

We are also echoing the values from the $post in the input field with a ternary operator and if validation fails then we will have old() value instead of $post values.

Lastly the update method of our TodosController which is same as store but we are updating an existing resource:public function update(Request $request, $id){ //validation rules $rules = [ 'title' => "required|string|unique:todos,title,{$id}|min:2|max:191", //Using double quotes 'body' => 'required|string|min:5|max:1000', ]; //custom validation error messages $messages = [ 'title.

unique' => 'Todo title should be unique', ]; //First Validate the form data $request->validate($rules,$messages); //Update the Todo $todo = Todo::findOrFail($id); $todo->title = $request->title; $todo->body = $request->body; $todo->save(); //Can be used for both creating and updating //Redirect to a specified route with flash message.

return redirect() ->route('todos.

show',$id) ->with('status','Updated the selected Todo!');}There is a scenario when you want to update the todo body but not title then you will get a validation error saying the title should be unique.

In these cases we can specify the id of that model to ignore.

Now we can create, display all todos, display a single todo, update them, and lastly delete them, Next tutorial will be on authentication and middleware.

Next tutorial will soon be published within this week.

Source code till this tutorial HERE | complete project HERE.

. More details

Leave a Reply