Code your dreams into reality.
Every line of code is a step towards a better future.
Embrace the bugs, they make you a better debugger.

Laravel 11 CRUD Application Tutorial

Last Updated on April 9, 2024 by

In this example tutorial, I will teach you how to create a CRUD operations application in Laravel 11 with a MySQL database.

CRUD means performing create, read, update and delete operations from the database. And CRUD is a basic requirement of any application.

Let’s build first simple laravel 11 CRUD operation application with mysql database step by step:

Step 1: Set Up Laravel 11 Project

If you do not have laravel 11 installed in your system, use the following command to install Laravel 11 using composer:

composer create-project --prefer-dist laravel/laravel crud-app

Step 2: Create Database Configuration

Set up your database configuration in the .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

Step 3: Generate Model and Migration

Generate a model and migration for your resource:

php artisan make:model Post -m

Open the migration file (database/migrations/xxxx_xx_xx_create_posts_table.php) and define the schema for your posts table:

    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('content');
            $table->timestamps();
        });
    }

Step 4: Run Migration

Run the migration to create the posts table in your database:

php artisan migrate

Step 5: Create Routes

Create routes in routes/web.php to handle CRUD operations application requests:

// routes/web.php

use App\Http\Controllers\PostController;

Route::resource('posts', PostController::class);

Step 6: Generate Controller

Generate a controller to handle your CRUD operations from database:

php artisan make:controller PostController --resource

Step 7: Implement Controller Methods

Implement the CRUD methods in your app/http/controllers/PostController.php to handle and perform crud operation from database:

// app/Http/Controllers/PostController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::latest()->paginate(10);
        return view('posts.index', compact('posts'));
    }

    public function create()
    {
        return view('posts.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required',
            'content' => 'required',
        ]);

        Post::create($request->all());

        return redirect()->route('posts.index')->with('success','Post created successfully.');
    }

    public function show(Post $post)
    {
        return view('posts.show', compact('post'));
    }

    public function edit(Post $post)
    {
        return view('posts.edit', compact('post'));
    }

    public function update(Request $request, Post $post)
    {
        $request->validate([
            'title' => 'required',
            'content' => 'required',
        ]);

        $post->update($request->all());

        return redirect()->route('posts.index')->with('success','Post updated successfully.');
    }

    public function destroy(Post $post)
    {
        $post->delete();

        return redirect()->route('posts.index')->with('success','Post deleted successfully.');
    }
}

Step 8: Create Views

Create Blade views for your CRUD operations in resources/views/posts directory:

  • index.blade.php
  • create.blade.php
  • edit.blade.php
  • show.blade.php

Edit index.blade.php file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laravel 11 CRUD Operation Tutorial - ITCODSTUFF.COM</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1 class="mt-5">Posts | Laravel 11 CRUD Operation Tutorial - ITCODSTUFF.COM</h1>
        <a href="{{ route('posts.create') }}" class="btn btn-primary mb-3">Create New Post</a>
        @if ($posts->count())
            <table class="table">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Content</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($posts as $post)
                        <tr>
                            <td>{{ $post->title }}</td>
                            <td>{{ $post->content }}</td>
                            <td>
                                <a href="{{ route('posts.show', $post->id) }}" class="btn btn-info btn-sm">View</a>
                                <a href="{{ route('posts.edit', $post->id) }}" class="btn btn-primary btn-sm">Edit</a>
                                <form action="{{ route('posts.destroy', $post->id) }}" method="POST" style="display: inline;">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this post?')">Delete</button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
            {{ $posts->links() }}
        @else
            <p>No posts found.</p>
        @endif
    </div>
</body>
</html>

Edit create.blade.php file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Post | Laravel 11 CRUD Operation Tutorial - ITCODSTUFF.COM</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1 class="mt-5">Create Post | Laravel 11 CRUD Operation Tutorial - ITCODSTUFF.COM</h1>
        <form action="{{ route('posts.store') }}" method="POST">
            @csrf
            <div class="mb-3">
                <label for="title" class="form-label">Title</label>
                <input type="text" class="form-control" id="title" name="title">
            </div>
            <div class="mb-3">
                <label for="content" class="form-label">Content</label>
                <textarea class="form-control" id="content" name="content"></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </div>
</body>
</html>

Edit edit.blade.php file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Post | Laravel 11 CRUD Operation Tutorial - ITCODSTUFF.COM</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1 class="mt-5">Edit Post | Laravel 11 CRUD Operation Tutorial - ITCODSTUFF.COM</h1>
        <form action="{{ route('posts.update', $post->id) }}" method="POST">
            @csrf
            @method('PUT')
            <div class="mb-3">
                <label for="title" class="form-label">Title</label>
                <input type="text" class="form-control" id="title" name="title" value="{{ $post->title }}">
            </div>
            <div class="mb-3">
                <label for="content" class="form-label">Content</label>
                <textarea class="form-control" id="content" name="content">{{ $post->content }}</textarea>
            </div>
            <button type="submit" class="btn btn-primary">Update</button>
        </form>
    </div>
</body>
</html>

Edit show.blade.php file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Show Post | Laravel 11 CRUD Operation Tutorial - ITCODSTUFF.COM</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1 class="mt-5">Show Post | Laravel 11 CRUD Operation Tutorial - ITCODSTUFF.COM</h1>
        <div class="card mt-3">
            <div class="card-body">
                <h5 class="card-title">{{ $post->title }}</h5>
                <p class="card-text">{{ $post->content }}</p>
                <a href="{{ route('posts.index') }}" class="btn btn-primary">Back</a>
            </div>
        </div>
    </div>
</body>
</html>

Step 9: Test

Run your Laravel application:

php artisan serve

Open browser with url http://localhost:8000/posts to test laravel 11 CRUD application.

That is all; Simple step-by-step guide taught you to make CRUD operation application in Laravel 11, which is the basic requirement of Laravel application.

Leave a Comment