Back to Blog
Lesson 31 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

Updating Existing Records: A Laravel CRUD Guide

Master updating existing records in Laravel. Learn how to bind Eloquent models to edit forms, handle PUT requests, and persist changes to your database.

LaravelCRUDEloquentMVCPHPbackend

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.

The Edit Workflow

To update an existing record, you generally need two routes:

  1. A GET route to display the "Edit" form.
  2. A PUT or PATCH route to process the form submission and save the changes.

Step 1: Creating the Edit Form

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.

HTML
style="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.

Step 2: Persisting the Update

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.

PHP
public 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.

Hands-on Exercise

  1. Open your TasksController and ensure you have an edit(Task $task) method that returns a view.
  2. Create the edit.blade.php file using the code block above as a template.
  3. Add a link in your task list view (from Task Manager: Displaying Real Database Records) that points to the edit route: <a href="/tasks/{{ $task->id }}/edit">Edit</a>.
  4. Submit the form and verify that the database table reflects your changes.

Common Pitfalls

  • Forgetting @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.
  • Mass Assignment Errors: If you try to update a field like 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.
  • Validation Overlap: Don't forget that updates often require different validation rules than creation (e.g., unique constraints that need to ignore the current record). Use the rule builder's ignore() method if you encounter this.

Recap

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.

Similar Posts