0
0
LaravelConceptBeginner · 3 min read

What is Resource Controller in Laravel: Explanation and Example

A resource controller in Laravel is a controller that handles all common actions for a resource, like creating, reading, updating, and deleting data, using predefined methods. It simplifies routing by automatically mapping HTTP verbs to controller methods following RESTful conventions.
⚙️

How It Works

A resource controller in Laravel works like a ready-made toolkit for managing data. Imagine you have a set of tasks to do repeatedly, like adding, viewing, editing, or deleting items. Instead of writing separate code for each task, Laravel provides a controller with built-in methods for these common actions.

When you create a resource controller, Laravel automatically connects specific URLs and HTTP methods (like GET, POST, PUT, DELETE) to the right method inside the controller. This means you don't have to manually define each route for every action. It follows a pattern called REST, which is like a universal language for web services.

Think of it as a restaurant menu where each dish corresponds to a specific action. The resource controller is the chef who knows exactly how to prepare each dish when ordered, making your code organized and easier to maintain.

💻

Example

This example shows how to create a resource controller for managing a list of books. The controller has methods for showing all books, creating a new book, updating, and deleting.

php
<?php

namespace App\Http\Controllers;

use App\Models\Book;
use Illuminate\Http\Request;

class BookController extends Controller
{
    public function index()
    {
        return Book::all();
    }

    public function store(Request $request)
    {
        return Book::create($request->all());
    }

    public function show($id)
    {
        return Book::findOrFail($id);
    }

    public function update(Request $request, $id)
    {
        $book = Book::findOrFail($id);
        $book->update($request->all());
        return $book;
    }

    public function destroy($id)
    {
        Book::destroy($id);
        return response(null, 204);
    }
}

// In routes/web.php or routes/api.php
use App\Http\Controllers\BookController;

Route::resource('books', BookController::class);
Output
GET /books returns all books as JSON POST /books creates a new book GET /books/{id} shows a single book PUT /books/{id} updates a book DELETE /books/{id} deletes a book
🎯

When to Use

Use a resource controller when you need to manage data that follows common actions like listing, creating, updating, and deleting. It is perfect for CRUD (Create, Read, Update, Delete) operations on resources such as users, posts, products, or orders.

For example, if you build a blog, a resource controller can handle all post-related actions without writing separate routes and methods for each. It saves time and keeps your code clean and consistent.

Key Points

  • A resource controller groups common actions for a resource in one place.
  • Laravel automatically maps HTTP verbs and URLs to controller methods.
  • It follows RESTful conventions for clean and predictable routing.
  • Use Route::resource() to register all routes for the controller.
  • Great for CRUD operations and organizing code efficiently.

Key Takeaways

A resource controller in Laravel handles all CRUD actions for a resource using predefined methods.
Use Route::resource() to automatically create routes linked to controller methods.
It follows RESTful patterns, making your code organized and easier to maintain.
Ideal for managing resources like posts, users, or products with common actions.
Simplifies routing by reducing the need to define each route manually.