Introduction
In Laravel, controllers are used to organize logic related to specific routes. A Resource Controller is a special type of controller that helps you quickly build CRUD (Create, Read, Update, Delete) operations in a structured way.
Why Use a Resource Controller?
- It follows Laravel’s RESTful design pattern.
- Reduces repetitive code.
- Automatically generates all necessary methods for CRUD.
- Keeps your code neat, clean, and easy to manage.
How to Create a Resource Controller?
You can create a resource controller using the Artisan command:
php artisan make:controller PostController --resource
This will create a file inside app/Http/Controllers/PostController.php with the following methods:
index()– Display a list of resources.create()– Show form to create new resource.store()– Save a new resource.show($id)– Display a single resource.edit($id)– Show form to edit a resource.update(Request $request, $id)– Update an existing resource.destroy($id)– Delete a resource.
Defining Routes for Resource Controller
In your routes/web.php, you can register a resource controller like this:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
This single line will create multiple routes for CRUD operations, such as:
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()
Example: Simple CRUD with Resource Controller
Let’s take an example of a Post resource.
Step 1: Create Migration and Model
php artisan make:model Post -m
Inside database/migrations/xxxx_create_posts_table.php:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
Run migration:
php artisan migrate
Step 2: Update PostController
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
// Show all posts
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
// Show create form
public function create()
{
return view('posts.create');
}
// Store new post
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.');
}
// Show single post
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
// Show edit form
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
// Update 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.');
}
// Delete post
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index')->with('success', 'Post deleted successfully.');
}
}
Step 3: Blade Views
You can create Blade templates like:
resources/views/posts/index.blade.phpresources/views/posts/create.blade.phpresources/views/posts/edit.blade.phpresources/views/posts/show.blade.php
Conclusion
Resource Controllers in Laravel are a powerful way to handle CRUD operations with minimal effort. Instead of writing separate routes and controller methods for each action, you can generate them in just one command. This makes your application more maintainable, scalable, and clean.