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:
- PHP >= 8.1
- Composer
- Laravel 12
- A database (MySQL/PostgreSQL etc.)
- A basic understanding of Laravel
Step 1: Create a New Laravel Project
Navigate to your project directory:
ποΈ Step 2: Configure Your .env
File
Update your .env
file with your database credentials:
Run migration setup:
π¦ Step 3: Create a Model and Migration
Letβs say we want to manage Products
.
In the migration file (database/migrations/xxxx_create_products_table.php
), define the schema:
Run the migration:
π§βπ¨ Step 4: Create a Controller
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:
πΌοΈ 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
You can repeat this structure for create.blade.php
, edit.blade.php
, etc.
π§ Step 7: Fill Controller Methods
Inside ProductController.php
:
Don't forget to add the fillable
fields in Product.php
:
π 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!