In this tutorial, you will learn how to build a complete Laravel CRUD application from scratch using Laravel 13, MySQL, Bootstrap 5, and Blade templates.
We will create a simple Student Management System that allows users to:
- Add new students.
- Display all student records.
- View individual student details.
- Edit existing student information.
- Delete student records.
- Validate form inputs.
- Store and retrieve information from MySQL.
By the end of this tutorial, you will understand how Laravel CRUD operations work in a real-world application.
What Is CRUD in Laravel?
CRUD stands for Create, Read, Update, and Delete. These are the four basic operations used to manage database records.
| Operation | Description | Laravel example |
|---|---|---|
| Create | Add a new record | Student::create() |
| Read | Retrieve records | Student::all() |
| Update | Modify an existing record | $student->update() |
| Delete | Remove a record | $student->delete() |
For example, a student management application uses CRUD operations to manage student information such as name, email, phone number, and course.
Technologies Used
We will use the following technologies:
- Laravel 13
- PHP 8.3 or higher
- MySQL
- Bootstrap 5
- HTML and CSS
- Blade templating engine
- Eloquent ORM
You can follow this tutorial using XAMPP or another compatible Laravel development environment.
Step 1: Create a New Laravel Project
First, open Command Prompt or the terminal in your development environment.
Navigate to your project directory:
cd C:\xampp\htdocs
Create a new Laravel application:
composer create-project laravel/laravel student-management
Navigate into the project:
cd student-management
Generate the application key:
php artisan key:generate
Start the Laravel development server:
php artisan serve
Open your browser and visit:
http://127.0.0.1:8000
You should see the Laravel welcome page.
Step 2: Create a MySQL Database
Open phpMyAdmin:
http://localhost/phpmyadmin
Create a new database named:
student_management
Now open the Laravel .env file and update the database configuration:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=student_management DB_USERNAME=root DB_PASSWORD=
Save the file.
These are typical local XAMPP credentials. If you configured a database password or another port, use your actual settings.
Step 3: Create a Student Model and Migration
Laravel uses models to interact with database tables.
Execute the following Artisan command:
php artisan make:model Student -m
This command creates:
- Student model.
- Student database migration.
Open the migration file inside:
database/migrations/
Find the migration with a name similar to:
create_students_table.php
Update its up() method:
public function up(): void
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('phone', 20)->nullable();
$table->string('course');
$table->timestamps();
});
}This migration creates the students table with the following fields:
| Field | Description |
|---|---|
| id | Unique student ID |
| name | Student name |
| email | Student email address |
| phone | Student phone number |
| course | Student course |
| created_at | Record creation date |
| updated_at | Last update date |
Run the migration:
php artisan migrate
Your students table will now be created in MySQL.
Step 4: Configure the Student Model
Open:
app/Models/Student.php
Update the model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $fillable = [
'name',
'email',
'phone',
'course',
];
}The $fillable property specifies which fields can be assigned through mass assignment methods such as create() and update().
Step 5: Create a Student Controller
A controller handles application requests and contains the business logic for CRUD operations.
Create a resource controller:
php artisan make:controller StudentController --resource
Laravel creates:
app/Http/Controllers/StudentController.php
Open the controller and add the following code:
<?php
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
class StudentController extends Controller
{
// Display all students
public function index()
{
$students = Student::latest()->paginate(10);
return view('students.index', compact('students'));
}
// Show the create form
public function create()
{
return view('students.create');
}
// Store a new student
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|max:255|unique:students,email',
'phone' => 'nullable|string|max:20',
'course' => 'required|string|max:255',
]);
Student::create($validated);
return redirect()
->route('students.index')
->with('success', 'Student added successfully.');
}
// Display one student
public function show(Student $student)
{
return view('students.show', compact('student'));
}
// Show the edit form
public function edit(Student $student)
{
return view('students.edit', compact('student'));
}
// Update student information
public function update(Request $request, Student $student)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|max:255|unique:students,email,' . $student->id,
'phone' => 'nullable|string|max:20',
'course' => 'required|string|max:255',
]);
$student->update($validated);
return redirect()
->route('students.index')
->with('success', 'Student updated successfully.');
}
// Delete a student
public function destroy(Student $student)
{
$student->delete();
return redirect()
->route('students.index')
->with('success', 'Student deleted successfully.');
}
}Understanding the Controller Methods
index(): Retrieves student records from the database and displays them with pagination.
create(): Displays the form for adding a new student.
store(): Validates the submitted information and creates a new database record.
show(): Displays an individual student's information.
edit(): Displays the form for editing an existing student.
update(): Validates and updates the selected student record.
destroy(): Deletes the selected student record.
Laravel's implicit route model binding automatically resolves the {student} parameter to a Student model instance.
Step 6: Create Laravel Routes
Open:
routes/web.php
Add the following code:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;
Route::get('/', function () {
return redirect()->route('students.index');
});
Route::resource('students', StudentController::class);The resource route automatically creates the routes required for CRUD operations.
You can check the generated routes using:
php artisan route:list
Laravel Resource Routes
| HTTP method | URL | Controller method | Purpose |
|---|---|---|---|
| GET | /students | index | List students |
| GET | /students/create | create | Show create form |
| POST | /students | store | Save student |
| GET | /students/{student} | show | View student |
| GET | /students/{student}/edit | edit | Show edit form |
| PUT/PATCH | /students/{student} | update | Update student |
| DELETE | /students/{student} | destroy | Delete student |
Step 7: Create the Blade View Directory
Inside the resources/views directory, create a folder named:
students
Create the following Blade files:
resources/views/
│
├── layouts/
│ └── app.blade.php
│
└── students/
├── index.blade.php
├── create.blade.php
├── edit.blade.php
└── show.blade.phpWe will use a common layout to avoid repeating the HTML structure on every page.
Step 8: Create the Main Blade Layout
Create:
resources/views/layouts/app.blade.php
Add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@yield('title', 'Student Management')</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body class="bg-light">
<nav class="navbar navbar-dark bg-dark">
<div class="container">
<a href="{{ route('students.index') }}"
class="navbar-brand">
Student Management
</a>
</div>
</nav>
<main class="container py-4">
@yield('content')
</main>
</body>
</html>This layout includes Bootstrap 5 and a common navigation bar.
Step 9: Create the Student Listing Page
Create:
resources/views/students/index.blade.php
Add:
@extends('layouts.app')
@section('title', 'Student List')
@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
<h2>Student List</h2>
<a href="{{ route('students.create') }}"
class="btn btn-primary">
Add Student
</a>
</div>
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
<div class="card shadow-sm">
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered table-striped align-middle">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Course</th>
<th width="220">Actions</th>
</tr>
</thead>
<tbody>
@forelse($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
<td>{{ $student->email }}</td>
<td>{{ $student->phone ?? '-' }}</td>
<td>{{ $student->course }}</td>
<td>
<a href="{{ route('students.show', $student) }}"
class="btn btn-info btn-sm">
View
</a>
<a href="{{ route('students.edit', $student) }}"
class="btn btn-warning btn-sm">
Edit
</a>
<form action="{{ route('students.destroy', $student) }}"
method="POST"
class="d-inline"
onsubmit="return confirm('Are you sure you want to delete this student?')">
@csrf
@method('DELETE')
<button type="submit"
class="btn btn-danger btn-sm">
Delete
</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="6" class="text-center">
No students found.
</td>
</tr>
@endforelse
</tbody>
</table>
</div>
{{ $students->links() }}
</div>
</div>
@endsectionThis page displays all student records and provides View, Edit, and Delete actions.
Step 10: Create the Add Student Form
Create:
resources/views/students/create.blade.php
Add:
@extends('layouts.app')
@section('title', 'Add Student')
@section('content')
<div class="card shadow-sm">
<div class="card-header">
<h4 class="mb-0">Add New Student</h4>
</div>
<div class="card-body">
<form action="{{ route('students.store') }}" method="POST">
@csrf
<div class="mb-3">
<label class="form-label">Student Name</label>
<input type="text"
name="name"
class="form-control @error('name') is-invalid @enderror"
value="{{ old('name') }}">
@error('name')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Email Address</label>
<input type="email"
name="email"
class="form-control @error('email') is-invalid @enderror"
value="{{ old('email') }}">
@error('email')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Phone Number</label>
<input type="text"
name="phone"
class="form-control @error('phone') is-invalid @enderror"
value="{{ old('phone') }}">
@error('phone')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Course</label>
<input type="text"
name="course"
class="form-control @error('course') is-invalid @enderror"
value="{{ old('course') }}">
@error('course')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-primary">
Save Student
</button>
<a href="{{ route('students.index') }}"
class="btn btn-secondary">
Cancel
</a>
</form>
</div>
</div>
@endsectionThis form uses Laravel validation error messages and the old() helper to retain submitted values when validation fails.
Step 11: Create the Edit Student Form
Create:
resources/views/students/edit.blade.php
Add:
@extends('layouts.app')
@section('title', 'Edit Student')
@section('content')
<div class="card shadow-sm">
<div class="card-header">
<h4 class="mb-0">Edit Student</h4>
</div>
<div class="card-body">
<form action="{{ route('students.update', $student) }}"
method="POST">
@csrf
@method('PUT')
<div class="mb-3">
<label class="form-label">Student Name</label>
<input type="text"
name="name"
class="form-control @error('name') is-invalid @enderror"
value="{{ old('name', $student->name) }}">
@error('name')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Email Address</label>
<input type="email"
name="email"
class="form-control @error('email') is-invalid @enderror"
value="{{ old('email', $student->email) }}">
@error('email')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Phone Number</label>
<input type="text"
name="phone"
class="form-control @error('phone') is-invalid @enderror"
value="{{ old('phone', $student->phone) }}">
@error('phone')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Course</label>
<input type="text"
name="course"
class="form-control @error('course') is-invalid @enderror"
value="{{ old('course', $student->course) }}">
@error('course')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-success">
Update Student
</button>
<a href="{{ route('students.index') }}"
class="btn btn-secondary">
Cancel
</a>
</form>
</div>
</div>
@endsectionThe old() helper displays previously submitted values if validation fails. Otherwise, the form displays the existing student information.
Step 12: Create the Student Details Page
Create:
resources/views/students/show.blade.php
Add:
@extends('layouts.app')
@section('title', 'Student Details')
@section('content')
<div class="card shadow-sm">
<div class="card-header">
<h4 class="mb-0">Student Details</h4>
</div>
<div class="card-body">
<p>
<strong>Name:</strong>
{{ $student->name }}
</p>
<p>
<strong>Email:</strong>
{{ $student->email }}
</p>
<p>
<strong>Phone:</strong>
{{ $student->phone ?? '-' }}
</p>
<p>
<strong>Course:</strong>
{{ $student->course }}
</p>
<p>
<strong>Created At:</strong>
{{ $student->created_at->format('d-m-Y h:i A') }}
</p>
<a href="{{ route('students.edit', $student) }}"
class="btn btn-warning">
Edit
</a>
<a href="{{ route('students.index') }}"
class="btn btn-secondary">
Back
</a>
</div>
</div>
@endsectionThis page displays the complete information for a selected student.
Step 13: Test the Laravel CRUD Application
Start your Laravel development server:
php artisan serve
Visit:
http://127.0.0.1:8000
Test each CRUD operation:
- Click Add Student and enter student information.
- Submit the form and verify the record appears in the listing.
- Click View to see the individual student details.
- Click Edit to modify the student information.
- Click Update Student to save the changes.
- Click Delete to remove a student record.
You now have a working Laravel CRUD application.
Understanding Laravel CRUD Architecture
Laravel follows the MVC architecture, which separates application responsibilities.
The request flow in our application is:
- The browser sends a request.
- Laravel matches the request with a route.
- The route calls the appropriate controller method.
- The controller validates and processes the request.
- The model communicates with the database.
- The controller returns a Blade view or redirects the visitor.
This separation makes applications easier to maintain, test, and extend.
Important Laravel CRUD Best Practices
When building CRUD applications for real-world projects, follow these practices:
1. Always Validate User Input
Never store unvalidated form data directly in the database. Use Laravel's validation system.
2. Use CSRF Protection
Include @csrf in forms that submit data using POST, PUT, PATCH, or DELETE requests.
3. Use Eloquent ORM
Use Eloquent models for common database operations instead of writing unnecessary raw SQL queries.
4. Use Pagination
For large database tables, use pagination instead of retrieving every record at once.
5. Use Authentication and Authorization
Protect sensitive CRUD operations with authentication and authorization policies or middleware.
6. Handle Errors Properly
Provide meaningful validation messages and handle missing records and unexpected errors appropriately.
7. Protect Against Unauthorized Deletion
Before deleting a record, verify that the logged-in user has permission to perform the operation.
8. Use Database Transactions When Necessary
For operations involving multiple related database changes, use transactions to maintain data consistency.
Frequently Asked Questions
1. What is CRUD in Laravel?
CRUD stands for Create, Read, Update, and Delete. Laravel uses routes, controllers, models, and database operations to manage records.
2. Which Laravel feature is used for database operations?
Laravel provides Eloquent ORM, which allows developers to interact with database tables using PHP models.
3. What is a resource controller in Laravel?
A resource controller is a controller that provides standard methods for CRUD operations, including index, create, store, show, edit, update, and destroy.
4. What is the purpose of Laravel migrations?
Migrations are used to create, modify, and manage database table structures using PHP code.
5. What is the difference between `save()` and `create()` in Laravel?
create() inserts a new record using mass assignment and requires the appropriate $fillable or $guarded configuration. save() persists a model instance after assigning its attributes.
6. What is the purpose of `@csrf` in Laravel?
The @csrf Blade directive generates a CSRF token field to protect forms against cross-site request forgery attacks.
7. What is route model binding in Laravel?
Route model binding automatically resolves a route parameter into the corresponding Eloquent model instance.
8. Can Laravel CRUD applications use AJAX?
Yes. Laravel CRUD operations can be implemented using AJAX or Fetch API requests, allowing users to add, update, and delete records without refreshing the entire page.
9. Can I use Bootstrap with Laravel?
Yes. Bootstrap can be used with Laravel Blade templates to create responsive forms, tables, buttons, navigation menus, and admin dashboards.
10. Can I build an admin panel using Laravel CRUD?
Yes. Laravel CRUD functionality is commonly used to develop admin panels for managing users, products, orders, categories, services, and website content.
Conclusion
Building a Laravel CRUD application is an excellent way to understand how Laravel works in practical development.
In this tutorial, we created a Student Management System using Laravel 13, MySQL, Bootstrap 5, Blade templates, and Eloquent ORM.
You learned how to create models, migrations, controllers, routes, forms, and database operations for managing student records.
Once you understand these fundamentals, you can extend the application with search, filters, image uploads, authentication, role-based permissions, AJAX operations, and REST APIs.
Keep practising by building real-world Laravel applications and gradually improving your development skills.
Need a Custom Laravel Web Application?
At 2K Web Solutions, we develop custom Laravel applications, admin panels, CRM systems, eCommerce platforms, booking systems, and business management solutions.
Explore our [Website Development Services](https://2kwebsolutions.com/website-development-services) or [contact us](https://2kwebsolutions.com/contact) to discuss your project requirements.