π What is CRUD?
CRUD stands for:
Create β Add new records
Read β View records
Update β Edit existing records
Delete β Remove records
CRUD is the backbone of any web application. Laravel makes building CRUD applications easy and structured.
βοΈ Step 1: Setup Laravel Project
If you havenβt already:
composer create-project laravel/laravel laravel-crud
cd laravel-crud
php artisan serve
Open browser: http://127.0.0.1:8000/
πΉ Step 2: Create Model & Migration
Weβll create a Post model:
php artisan make:model Post -m
-mβ generates a migration file automatically
Edit migration file database/migrations/xxxx_create_posts_table.php:
public function up() {
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
Run migration:
php artisan migrate
πΉ Step 3: Create Controller
php artisan make:controller PostController --resource
--resourceβ generates all CRUD methods automatically
πΉ Step 4: Define Routes
In routes/web.php:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
This creates all 7 RESTful routes for CRUD:
| Method | Route | Action |
| --------- | ---------------- | ------- |
| GET | /posts | index |
| GET | /posts/create | create |
| POST | /posts | store |
| GET | /posts/{id} | show |
| GET | /posts/{id}/edit | edit |
| PUT/PATCH | /posts/{id} | update |
| DELETE | /posts/{id} | destroy |
