Master updating existing records in Laravel. Learn how to bind Eloquent models to edit forms, handle PUT requests, and persist changes to your database.
Previously in this course, we covered Introduction to Route Model Binding in Laravel, which allows us to automatically fetch database records based on URL parameters. In this lesson, we will use that skill to implement the "Update" functionality for our Task Manager.
Updating records is the third step in the classic CRUD (Create, Read, Update, Delete) cycle. While creating records involves sending new data to the server, updating requires a two-step dance: displaying an existing record in a form, then sending those modified values back to the server to persist changes.
To update an existing record, you generally need two routes:
GET route to display the "Edit" form.PUT or PATCH route to process the form submission and save the changes.In your tasks/edit.blade.php view, you need to populate your form inputs with the current values of the task. Because we are using Route Model Binding, our controller will pass a $task object to the view.
HTMLstyle="color:#808080"><style="color:#4EC9B0">form action="/tasks/{{ $task->id }}" method="POST"> @csrf @method('PUT') style="color:#808080"><style="color:#4EC9B0">input type="text" name="title" value="{{ old('title', $task->title) }}"> style="color:#808080"><style="color:#4EC9B0">textarea name="description">{{ old('description', $task->description) }}style="color:#808080"></style="color:#4EC9B0">textarea> style="color:#808080"><style="color:#4EC9B0">button type="submit">Update Taskstyle="color:#808080"></style="color:#4EC9B0">button> style="color:#808080"></style="color:#4EC9B0">form>
Notice the @method('PUT') directive. Since standard HTML forms only support GET and POST, Laravel uses this hidden field to simulate a PUT request, which is the RESTful standard for updates.
In your TasksController, you will handle the submission. You should follow the principles we discussed in Performing Basic CRUD Operations in Laravel with Eloquent and Preventing Mass Assignment in Laravel.
PHPpublic function update(Request $request, Task $task) { #6A9955">// 1. Validate the incoming data $validated = $request->validate([ 'title' => 'required|max:255', 'description' => 'nullable', ]); #6A9955">// 2. Update the model instance $task->update($validated); #6A9955">// 3. Redirect the user return redirect('/tasks')->with('success', 'Task updated successfully!'); }
The $task->update() method is a powerful Eloquent feature. It automatically filters the input array against your model's $fillable property and saves only the permitted fields to the database.
TasksController and ensure you have an edit(Task $task) method that returns a view.edit.blade.php file using the code block above as a template.<a href="/tasks/{{ $task->id }}/edit">Edit</a>.@method('PUT'): If you omit this, Laravel will treat the form as a POST request, which usually results in a 405 Method Not Allowed error because your route is defined as PUT or PATCH.user_id that isn't in your $fillable array, Laravel will silently ignore it for security reasons. Always double-check your model's fillable configuration if data isn't saving.ignore() method if you encounter this.Updating records relies on retrieving the correct model instance, injecting it into a view pre-populated with current data, and using the update() method to persist modifications. By leveraging Eloquent's mass-assignment protection and route model binding, you keep your code clean and secure.
Up next: Deleting Records — how to remove data safely and clean up your database.
Master database operations with our guide to CRUD in Laravel. Learn how to save, fetch, update, and delete records using the powerful Eloquent ORM.
Read moreLearn how to use Eloquent, Laravel's powerful ORM, to interact with database tables as clean, object-oriented models for your Task Manager application.
Updating Existing Records
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation