If you're just starting your journey with Laravel, one of the best beginner projects you can build is a CRUD app. It covers all the fundamental concepts you'll use in real-world Laravel development: routing, controllers, database operations, views, validation, and more.
In this tutorial, Iโll walk you through how to build a simple Post Management System that lets you Create, Read, Update, and Delete posts.
Letโs dive in ๐
โ What Youโll Learn
- How Laravel routes and controllers work
- How to connect and use a database with Laravel
- How to use resource controllers
- How to build forms using Blade templates
- How to validate and persist form data
๐ง Tools & Prerequisites
Before we begin, make sure you have:
- PHP 8.x installed
- Composer installed
- Laravel CLI installed (
composer global require laravel/installer
) - A database (MySQL or SQLite)
- VS Code or your favorite editor
๐ Step 1: Create a New Laravel Project
laravel new crud-app
cd crud-app
Or using Composer:
composer create-project laravel/laravel crud-app
โ๏ธ Step 2: Configure Your Database
Open the .env
file and set up your MySQL or SQLite credentials:
DB_DATABASE=crud_app
DB_USERNAME=root
DB_PASSWORD=secret
Then run:
php artisan migrate
โจ Step 3: Create a Model, Controller, and Migration
Weโll use Artisan to generate everything in one go:
php artisan make:model Post -mcr
This creates:
- A
Post
model - A migration file
- A resource controller (
PostController
)
๐งฑ Step 4: Define the Posts Table
Open the migration file in database/migrations/..._create_posts_table.php
and add:
$table->string('title');
$table->text('content');
Run the migration:
php artisan migrate
๐บ๏ธ Step 5: Set Up Routes
In routes/web.php
, register resourceful routes:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
This creates all 7 routes automatically:
- index
- create
- store
- show
- edit
- update
- destroy
๐ง Step 6: Build Out the Controller Logic
Inside PostController.php
, add logic to handle CRUD:
public function index()
{
$posts = Post::latest()->get();
return view('posts.index', compact('posts'));
}
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$data = $request->validate([
'title' => 'required|max:255',
'content' => 'required',
]);
Post::create($data);
return redirect()->route('posts.index')->with('success', 'Post created successfully!');
}
Repeat similar logic for edit
, update
, and destroy
.
๐จ Step 7: Create Blade Views
In resources/views/posts/
, create these files:
-
index.blade.php
โ list all posts -
create.blade.php
โ form to create -
edit.blade.php
โ form to edit
Example for index.blade.php
:
@foreach ($posts as $post)
<h2>{{ $post->title }}</h2>
<p>{{ $post->content }}</p>
<a href="{{ route('posts.edit', $post) }}">Edit</a>
<form action="{{ route('posts.destroy', $post) }}" method="POST">
@csrf @method('DELETE')
<button>Delete</button>
</form>
@endforeach
๐ Final Touch: Validation + Flash Messages
Use Laravel's built-in validation and flash messaging:
return redirect()->route('posts.index')->with('success', 'Post updated!');
And display the message in your Blade:
@if(session('success'))
<p>{{ session('success') }}</p>
@endif
๐งฉ Whatโs Next?
You just built your first full-stack Laravel CRUD app! ๐
Here are some challenges to take it further:
- Add Laravel Breeze for authentication
- Implement file uploads for featured images
- Add search and pagination
๐ Bonus: GitHub Code
The full working source code is available here:
๐ github.com/rohitdhiman91/laravel-crud-app
๐ Questions?
Feel free to leave a comment, connect with me on LinkedIn, or DM me on Twitter. Happy to help!
๐ Conclusion
Starting with a CRUD app is the best way to learn Laravel fundamentals. Once you get comfortable, you'll be ready to build more complex apps like blogs, dashboards, and eCommerce platforms.
Thanks for reading! If you found this helpful, consider sharing it ๐
๐ท๏ธ Tags
#Laravel
#PHP
#CRUD
#WebDevelopment
#Beginners
#Fullstack
#BackendDevelopment
#LaravelTutorial
Top comments (0)