Laravel : CRUD update a form
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering CRUD in Laravel: Updating Forms Seamlessly
As a senior developer, I frequently encounter questions about managing data persistence in frameworks like Laravel. The desire to move from simply creating data to fully mastering the Create, Read, Update, and Delete cycle—especially handling complex form submissions—is fundamental.
You are wrestling with a very common hurdle: successfully mapping user input from an HTML form back into your Eloquent model and correctly executing the database update. The error messages you encountered (methodNotAllowed) often point to routing or authorization issues, but the core problem here lies in the interaction between your controller logic and your Blade view when handling updates.
This guide will walk you through the correct, robust way to implement a full CRUD update process in Laravel, addressing the specific challenges in your provided code. We will focus on best practices for handling form data, validation, and Eloquent updates.
Understanding the Laravel Update Flow (The Controller Side)
The key to a successful update lies within your controller method. When handling an update request, you need three main steps: fetch the record, validate the incoming data, and then save the changes back to the database.
Let's look at how your update method in AdminController should be structured. We will ensure we are using the $request->all() method correctly for mass assignment and proper Eloquent interaction.
Refactoring the Update Logic
In Laravel, the controller acts as the intermediary between the HTTP request and the model.
use Illuminate\Http\Request;
use App\Models\Student; // Assuming your model is Student
class AdminController extends Controller
{
// ... index, create, edit methods remain ...
public function update(Request $request, $id)
{
// 1. Validation: Ensure the incoming data meets necessary criteria.
$request->validate([
'firstname' => 'required|string|max:255',
'lastname' => 'required|string|max:255',
]);
// 2. Find the record: Locate the student to be updated.
$student = Student::findOrFail($id); // Use findOrFail for better error handling
// 3. Update the attributes: Assign the new values from the request.
$student->firstname = $request->input('firstname');
$student->lastname = $request->input('lastname');
// 4. Save the changes: Commit the transaction to the database.
$student->save();
// 5. Redirect with Feedback: Always redirect after a successful operation.
return redirect()->route('student.index')
->with('success', 'Student updated successfully!');
}
}
Why this approach? By using $request->input('field_name') or $request->all(), you are explicitly pulling the data the user submitted. This ensures that if you were to implement authorization later (e.g., checking if the user owns the record), you have clear, validated inputs ready for comparison. Remember, strong validation is one of Laravel's greatest strengths!
Binding Data Correctly in the View (The Blade Side)
The confusion often arises when passing data from the controller to the view, and then submitting it back correctly. In your edit.blade.php, the form must accurately reflect the data you are editing, and the input names (name attributes) must match what the controller expects.
Correcting the Edit Form
When creating a form for editing, you should use the $student object to populate the fields:
@section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1>Edit {{ $student->firstname }}</h1>
</div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
{{-- The action points to the correct route --}}
<form class="panel-body" action="{{ route('student.update', $student->id) }}" method="POST">
@csrf
{{-- Input for Firstname --}}
<fieldset class="form-group">
<label for="firstname">Firstname</label>
{{-- Use the actual data from the model --}}
<input type="text" name="firstname" class="form-control" id="firstname" value="{{ $student->firstname }}">
</fieldset>
{{-- Input for Lastname --}}
<fieldset class="form-group">
<label for="lastname">Lastname</label>
<input type="text" name="lastname" class="form-control" id="lastname" value="{{ $student->lastname }}">
</fieldset>
{{-- Submit Button --}}
<button type="submit" class="btn btn-primary pull-right">Save Changes</button>
</form>
</div>
</div>
</div>
</div>
@endsection
Notice the difference: we are using name="firstname" and name="lastname" in the form. When the form is submitted via POST, Laravel automatically maps these names to the request data, making the controller's validation and update process straightforward and reliable.
Conclusion: Embracing Eloquent for Robust CRUD
The journey