Introduction
CRUD stands for Create, Read, Update, and Delete. These are the four basic operations used in almost every web application. Whether you are building a blog, admin panel, or management system, CRUD operations are essential.
In this tutorial, we will learn how to build a simple CRUD system in Laravel using Models, Controllers, Routes, and Views.
Step 1: Create Model and Migration
First, create a model with migration using Artisan:
php artisan make:model Post -m
This command creates:
- Model:
app/Models/Post.php - Migration file inside
database/migrations
Now open the migration file and add fields:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
Run migration:
php artisan migrate
Step 2: Create Controller
Create a resource controller:
php artisan make:controller PostController --resource
This generates methods for CRUD:
- index()
- create()
- store()
- show()
- edit()
- update()
- destroy()
Step 3: Define Routes
Open routes/web.php:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
Laravel automatically creates all CRUD routes.
Step 4: Create Operation (Store Data)
use App\Models\Post;
use Illuminate\Http\Request;
public function store(Request $request)
{
Post::create([
'title' => $request->title,
'content' => $request->content
]);
return redirect()->route('posts.index');
}
Step 5: Read Operation (Fetch Data)
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
Step 6: Update Operation
public function update(Request $request, Post $post)
{
$post->update([
'title' => $request->title,
'content' => $request->content
]);
return redirect()->route('posts.index');
}
Step 7: Delete Operation
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index');
}
Step 8: Enable Mass Assignment
Inside Post.php model:
protected $fillable = ['title', 'content'];
This allows Laravel to insert and update data securely.
Why CRUD Is Important in Laravel
- Foundation of dynamic applications
- Helps manage database records easily
- Reduces manual SQL queries
- Uses clean MVC architecture
- Makes applications scalable and maintainable
Conclusion
CRUD operations are one of the most important concepts in Laravel development. By combining Models, Controllers, Routes, and Views, you can build powerful database-driven applications quickly. Once you understand CRUD, you are ready to move toward advanced topics like validation, middleware, and authentication.
Category: Laravel
Tags: #laravel database, #Laravel database models, #Laravel database interaction, #laravel crud tutorial, #laravel crud example beginner, #laravel create read update delete, #laravel eloquent crud tutorial, #laravel beginner project, #laravel database operations, #laravel controller crud example, #php laravel tutorial beginners, #laravel full crud example, #laravel web development guide
2 Likes 0 Comments 6 Views
Leave a comment
Leave a Reply