DEV Community

Cover image for Building Your First CRUD App in Laravel: A Step-by-Step Guide for Beginners
Rohit Dhiman
Rohit Dhiman

Posted on

Building Your First CRUD App in Laravel: A Step-by-Step Guide for Beginners

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
Enter fullscreen mode Exit fullscreen mode

Or using Composer:

composer create-project laravel/laravel crud-app
Enter fullscreen mode Exit fullscreen mode

โš™๏ธ 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
Enter fullscreen mode Exit fullscreen mode

Then run:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

โœจ Step 3: Create a Model, Controller, and Migration

Weโ€™ll use Artisan to generate everything in one go:

php artisan make:model Post -mcr
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

Run the migration:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

๐Ÿ—บ๏ธ Step 5: Set Up Routes

In routes/web.php, register resourceful routes:

use App\Http\Controllers\PostController;

Route::resource('posts', PostController::class);
Enter fullscreen mode Exit fullscreen mode

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!');
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

๐ŸŒŸ Final Touch: Validation + Flash Messages

Use Laravel's built-in validation and flash messaging:

return redirect()->route('posts.index')->with('success', 'Post updated!');
Enter fullscreen mode Exit fullscreen mode

And display the message in your Blade:

@if(session('success'))
    <p>{{ session('success') }}</p>
@endif
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฉ 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)