Laravel 12 Crud Operations Step By Step Guide For Beginners

image
image
image
image
image
image
image
image
Laravel 12 CRUD Operations: Step-by-Step Guide for Beginners

Laravel 12 CRUD Operations: Step-by-Step Guide for Beginners

Laravel 12, the latest version of the powerful PHP framework, continues to simplify web development with its elegant syntax and expressive features. If you're looking to build Create, Read, Update, and Delete (CRUD) functionality into your Laravel application, this guide is for you.


In this blog post, we'll walk through the entire process of creating a CRUD application in Laravel 12 from scratch.


πŸ› οΈ Prerequisites


Before getting started, make sure you have the following installed:

  1. PHP >= 8.1
  2. Composer
  3. Laravel 12
  4. A database (MySQL/PostgreSQL etc.)
  5. A basic understanding of Laravel


Step 1: Create a New Laravel Project

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


Navigate to your project directory:

bash
CopyEdit
cd laravel-crud-app

πŸ—ƒοΈ Step 2: Configure Your .env File

Update your .env file with your database credentials:

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

Run migration setup:

bash
CopyEdit
php artisan migrate

πŸ“¦ Step 3: Create a Model and Migration

Let’s say we want to manage Products.

bash
CopyEdit
php artisan make:model Product -m

In the migration file (database/migrations/xxxx_create_products_table.php), define the schema:

php
CopyEdit
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->decimal('price', 8, 2);
$table->timestamps();
});
}

Run the migration:

bash
CopyEdit
php artisan migrate

πŸ§‘β€πŸŽ¨ Step 4: Create a Controller

bash
CopyEdit
php artisan make:controller ProductController --resource

This command creates a controller with all basic CRUD methods (index, create, store, show, edit, update, destroy).

πŸ–ŠοΈ Step 5: Define Routes

Open routes/web.php and add:

php
CopyEdit
use App\Http\Controllers\ProductController;

Route::resource('products', ProductController::class);

πŸ–ΌοΈ Step 6: Create Views with Blade

Create a folder in resources/views/products and add the following Blade templates:

1. index.blade.php – List All Products

2. create.blade.php – Create Product

3. edit.blade.php – Edit Product

4. show.blade.php – Show Product

Example: index.blade.php

blade
CopyEdit
@extends('layout')

@section('content')
<h1>All Products</h1>
<a href="{{ route('products.create') }}">Add Product</a>
<ul>
@foreach ($products as $product)
<li>
{{ $product->name }} - ${{ $product->price }}
<a href="{{ route('products.edit', $product->id) }}">Edit</a>
<form action="{{ route('products.destroy', $product->id) }}" method="POST" style="display:inline;">
@csrf
@method('DELETE')
<button>Delete</button>
</form>
</li>
@endforeach
</ul>
@endsection

You can repeat this structure for create.blade.php, edit.blade.php, etc.

🧠 Step 7: Fill Controller Methods

Inside ProductController.php:

php
CopyEdit
use App\Models\Product;
use Illuminate\Http\Request;

public function index() {
$products = Product::all();
return view('products.index', compact('products'));
}

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

public function store(Request $request) {
$request->validate([
'name' => 'required',
'price' => 'required|numeric',
]);

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

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

public function edit(Product $product) {
return view('products.edit', compact('product'));
}

public function update(Request $request, Product $product) {
$request->validate([
'name' => 'required',
'price' => 'required|numeric',
]);

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

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

public function destroy(Product $product) {
$product->delete();

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

Don't forget to add the fillable fields in Product.php:

php
CopyEdit
protected $fillable = ['name', 'description', 'price'];

πŸŽ‰ Done!

You now have a fully functional CRUD application in Laravel 12. You can enhance it with validation, search, pagination, authentication, and more.

πŸš€ Final Thoughts

This CRUD system forms the foundation for many real-world web applications. Once you master this, you're well on your way to building powerful Laravel apps.

If you found this helpful, don’t forget to share and bookmark this guide!