🚀 Laravel 12 CRUD Application Example Tutorial

Laravel 12 is the latest and most powerful version of the popular PHP framework that simplifies web development with its elegant syntax and built-in features. In this tutorial, we'll walk through how to build a complete CRUD (Create, Read, Update, Delete) application using Laravel 12 step-by-step.

Whether you're a beginner or want to refresh your Laravel basics, this guide is designed to help you master the core concepts of Laravel 12 development.


🧰 What You'll Learn

  • Installing Laravel 12
  • Database configuration
  • Creating Model, Migration, and Controller
  • Building Blade views
  • Writing routes for CRUD operations
  • Validating form input
  • Testing the application


🖥️ Step 1: Install Laravel 12

First, make sure you have Composer installed.

composer create-project laravel/laravel laravel12-crud

Navigate into the project:

cd laravel12-crud

Run the built-in Laravel server:

php artisan serve

⚙️ Step 2: Setup the Database

Open .env file and configure your database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_crud
DB_USERNAME=root
DB_PASSWORD=

Then, create the database in MySQL (e.g., via phpMyAdmin or command line).


🧱 Step 3: Create Model, Migration & Controller

Let’s say we're building a Post manager.

Run this artisan command:

php artisan make:model Post -mcr

This will create:

  • Model: app/Models/Post.php
  • Migration: database/migrations/xxxx_create_posts_table.php
  • Controller: app/Http/Controllers/PostController.php

Edit the migration file to add columns:

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

Then migrate:

php artisan migrate

✍️ Step 4: Define Routes

Open routes/web.php and add:

use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);

This creates all necessary CRUD routes.


🧠 Step 5: Controller Logic

Open PostController.php and define logic:

use App\Models\Post;
use Illuminate\Http\Request;
public function index()
{
$posts = Post::latest()->paginate(5);
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 6: Setup Blade Views

1. resources/views/posts/layout.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Laravel 12 CRUD</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-4">
@yield('content')
</div>
</body>
</html>

2. resources/views/posts/index.blade.php

@extends('posts.layout')
@section('content')
<div class="d-flex justify-content-between">
<h2>Laravel 12 CRUD - Posts</h2>
<a class="btn btn-success" href="{{ route('posts.create') }}">Create Post</a>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success mt-2">{{ $message }}</div>
@endif
<table class="table table-bordered mt-3">
<tr>
<th>No</th>
<th>Title</th>
<th>Content</th>
<th width="280px">Action</th>
</tr>
@foreach ($posts as $post)
<tr>
<td>{{ ++$loop->index }}</td>
<td>{{ $post->title }}</td>
<td>{{ Str::limit($post->content, 50) }}</td>
<td>
<form action="{{ route('posts.destroy',$post->id) }}" method="POST">
<a class="btn btn-info btn-sm" href="{{ route('posts.show',$post->id) }}">Show</a>
<a class="btn btn-primary btn-sm" href="{{ route('posts.edit',$post->id) }}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
{!! $posts->links() !!}
@endsection

3. resources/views/posts/create.blade.php

@extends('posts.layout')
@section('content')
<h2>Create New Post</h2>
@if ($errors->any())
<div class="alert alert-danger">
<ul>@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach</ul>
</div>
@endif
<form action="{{ route('posts.store') }}" method="POST">
@csrf
<div class="mb-3">
<label>Title:</label>
<input type="text" name="title" class="form-control" placeholder="Title">
</div>
<div class="mb-3">
<label>Content:</label>
<textarea class="form-control" name="content" placeholder="Content"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
@endsection

4. resources/views/posts/edit.blade.php

Same as create, but pre-filled:

@extends('posts.layout')
@section('content')
<h2>Edit Post</h2>
@if ($errors->any())
<div class="alert alert-danger">
<ul>@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach</ul>
</div>
@endif
<form action="{{ route('posts.update', $post->id) }}" method="POST">
@csrf
@method('PUT')
<div class="mb-3">
<label>Title:</label>
<input type="text" name="title" value="{{ $post->title }}" class="form-control">
</div>
<div class="mb-3">
<label>Content:</label>
<textarea class="form-control" name="content">{{ $post->content }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection

5. resources/views/posts/show.blade.php

@extends('posts.layout')
@section('content')
<h2>{{ $post->title }}</h2>
<p>{{ $post->content }}</p>
<a class="btn btn-secondary mt-3" href="{{ route('posts.index') }}">Back</a>
@endsection

🧪 Step 7: Run and Test

Make sure your development server is running:

php artisan serve

Now visit:
➡️ http://127.0.0.1:8000/posts

You should be able to:

  • Create a new post
  • See the list of posts
  • View a single post
  • Edit a post
  • Delete a post

Post a Comment

Previous Post Next Post

Contact Form