Lesson 26 of 50 - Learn how to create, read, update, and delete database records using Laravel Eloquent.
CRUD represents the four basic operations used to manage database records in an application.
| Operation | Meaning | Example |
|---|---|---|
| Create | Insert a new record. | Add a student. |
| Read | Retrieve existing records. | Display all students. |
| Update | Modify an existing record. | Change a student's course. |
| Delete | Remove a record. | Delete a student. |
In this lesson, we will build a student CRUD system using Laravel routes, a controller, a model, and Blade views.
Run this Artisan command:
php artisan make:model Student -mcr
This creates:
Open the generated students table migration and define the required columns.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('course');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('students');
}
};
Run the migration:
php artisan migrate
If your project already has a students table, adapt your existing schema instead of creating a duplicate.
Open app/Models/Student.php and configure
the attributes that can be assigned through
create() and update().
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
use HasFactory;
protected $fillable = [
'name',
'email',
'course',
];
}
The $fillable property allows the listed
attributes to be mass-assigned.
Make sure the field names match the columns in your students table.
Open routes/web.php and add the following
resource route.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;
Route::resource('students', StudentController::class);
This registers the standard CRUD routes for the StudentController.
| HTTP Method | URL | Controller Method |
|---|---|---|
| GET | /students | index() |
| GET | /students/create | create() |
| POST | /students | store() |
| GET | /students/{student}/edit | edit() |
| PUT/PATCH | /students/{student} | update() |
| DELETE | /students/{student} | destroy() |
| GET | /students/{student} | show() |
You can view registered routes using:
php artisan route:list --path=students
Open app/Http/Controllers/StudentController.php.
Replace the generated controller methods with the following
complete CRUD implementation.
<?php
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class StudentController extends Controller
{
// Display all students
public function index(): View
{
$students = Student::latest()->paginate(10);
return view('students.index', compact('students'));
}
// Show the create form
public function create(): View
{
return view('students.create');
}
// Store a new student
public function store(Request $request): RedirectResponse
{
$validated = $request->validate([
'name' => 'required|string|max:100',
'email' => 'required|email|max:255|unique:students,email',
'course' => 'required|string|max:100',
]);
Student::create($validated);
return redirect()
->route('students.index')
->with('success', 'Student added successfully!');
}
// Display one student
public function show(Student $student): View
{
return view('students.show', compact('student'));
}
// Show the edit form
public function edit(Student $student): View
{
return view('students.edit', compact('student'));
}
// Update a student
public function update(
Request $request,
Student $student
): RedirectResponse {
$validated = $request->validate([
'name' => 'required|string|max:100',
'email' => 'required|email|max:255|unique:students,email,' . $student->id,
'course' => 'required|string|max:100',
]);
$student->update($validated);
return redirect()
->route('students.index')
->with('success', 'Student updated successfully!');
}
// Delete a student
public function destroy(Student $student): RedirectResponse
{
$student->delete();
return redirect()
->route('students.index')
->with('success', 'Student deleted successfully!');
}
}
Laravel's implicit route model binding resolves the
$student parameter from the route.
The controller validates submitted data before saving it.
This example uses the default integer primary key named
id. Add authentication and authorization
before allowing real users to manage student records.
Create the directory
resources/views/students.
Create resources/views/students/index.blade.php.
<h2>Student Management</h2>
@if (session('success'))
<div style="color:green;">
{{ session('success') }}
</div>
@endif
<a href="{{ route('students.create') }}">
Add New Student
</a>
<table border="1" cellpadding="10">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Course</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@forelse ($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
<td>{{ $student->email }}</td>
<td>{{ $student->course }}</td>
<td>
<a href="{{ route('students.show', $student) }}">
View
</a>
<a href="{{ route('students.edit', $student) }}">
Edit
</a>
<form action="{{ route('students.destroy', $student) }}"
method="POST"
style="display:inline;">
@csrf
@method('DELETE')
<button type="submit"
onclick="return confirm('Delete this student?')">
Delete
</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="5">No students found.</td>
</tr>
@endforelse
</tbody>
</table>
{{ $students->links() }}
The listing displays student records, provides View and Edit links, and submits a DELETE request when the user confirms deletion.
Create
resources/views/students/create.blade.php
for adding students.
<h2>Add Student</h2>
@if ($errors->any())
<ul style="color:red;">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
<form action="{{ route('students.store') }}" method="POST">
@csrf
<label>Name:</label>
<input type="text" name="name"
value="{{ old('name') }}">
<br><br>
<label>Email:</label>
<input type="email" name="email"
value="{{ old('email') }}">
<br><br>
<label>Course:</label>
<input type="text" name="course"
value="{{ old('course') }}">
<br><br>
<button type="submit">Save Student</button>
</form>
Now create
resources/views/students/edit.blade.php.
<h2>Edit Student</h2>
@if ($errors->any())
<ul style="color:red;">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
<form action="{{ route('students.update', $student) }}"
method="POST">
@csrf
@method('PUT')
<label>Name:</label>
<input type="text" name="name"
value="{{ old('name', $student->name) }}">
<br><br>
<label>Email:</label>
<input type="email" name="email"
value="{{ old('email', $student->email) }}">
<br><br>
<label>Course:</label>
<input type="text" name="course"
value="{{ old('course', $student->course) }}">
<br><br>
<button type="submit">Update Student</button>
</form>
The create form sends a POST request. The edit form
uses @method('PUT') to submit a PUT
request through an HTML form.
Create
resources/views/students/show.blade.php.
<h2>Student Details</h2>
<p>
<strong>ID:</strong>
{{ $student->id }}
</p>
<p>
<strong>Name:</strong>
{{ $student->name }}
</p>
<p>
<strong>Email:</strong>
{{ $student->email }}
</p>
<p>
<strong>Course:</strong>
{{ $student->course }}
</p>
<a href="{{ route('students.index') }}">
Back to Students
</a>
The controller's show() method passes
the selected Student model to this view.
Laravel's normal Blade output syntax escapes displayed values to help prevent HTML injection.
CRUD operations in Laravel:
| Operation | HTTP Method | Controller Method |
|---|---|---|
| Create form | GET | create() |
| Save record | POST | store() |
| List records | GET | index() |
| View record | GET | show() |
| Edit form | GET | edit() |
| Update record | PUT/PATCH | update() |
| Delete record | DELETE | destroy() |
Practical Assignment:
Test the complete workflow using a development database before using it with real student information.
1. What does CRUD stand for?
2. Which Blade directive includes a CSRF token in a form?
3. Which HTTP method is commonly used to delete a resource?