Build Your First Laravel CRUD Application
Learn to build a complete Create, Read, Update, Delete application in Laravel 12, step by step, from routing to forms.
Learning objectives
By the end of this tutorial you will be able to:
- Set up a Laravel project and understand its structure
- Create models, migrations, and controllers
- Build CRUD routes and Blade views
- Add form validation
- Understand the MVC pattern
Prerequisites
- Basic PHP knowledge
- Laravel installed locally
- A code editor
Getting Started
In this tutorial you will build a simple blog post manager using Laravel. This is a classic school-project style application: you list posts, create them, edit them, and delete them.
1. Create the project
composer create-project laravel/laravel blog
cd blog
php artisan migrate
The migrate command sets up the default tables using the SQLite database configured in your .env file.
Why this matters: Running migrations create a clean, version-controlled database schema every time.
2. Create the Post model and migration
php artisan make:model Post -m
Open the migration file and add your columns:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
Then run php artisan migrate again to create the posts table.
3. Generate the controller
php artisan make:controller PostController --resource
A resource controller gives you index, create, store, show, edit, update, and destroy methods.
4. Add the routes
Route::resource('posts', PostController::class);
This single line creates all seven RESTful routes for posts.
5. Implement the controller methods
public function index()
{
$posts = Post::all();
return view('posts.index', ['posts' => $posts]);
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'body' => 'required|string',
]);
Post::create($validated);
return redirect()->route('posts.index');
}
Validation is essential — never trust user input. The validate() call guarantees title and body are present and correctly typed before anything is saved.
6. Build the views
Create resources/views/posts/index.blade.php:
@foreach ($posts as $post)
<div>
<h2>{{ $post->title }}</h2>
<p>{{ $post->body }}</p>
<a href="{{ route('posts.edit', $post) }}">Edit</a>
<form method="POST" action="{{ route('posts.destroy', $post) }}">
@csrf
@method('DELETE')
<button type="submit">Delete</button>
</form>
</div>
@endforeach
Notice the @csrf directive — this is Laravel's built-in protection against cross-site request forgery.
7. Test your application
Run the development server and try adding, editing, and deleting posts.
php artisan serve
Wrapping up
You have built a complete CRUD application. Try extending it by adding authentication, categories, or search — great additions for a school project.
DOK-WEB Team
Software Engineering Team
The DOK-WEB engineering team — building practical tutorials from real project experience.