Laravel for Beginners – Learn to Build Full-Stack Web Apps

Loading video...

Resource Controllers

Controllers

Resource Controllers

In Laravel, a resource controller is a special type of controller that provides a convenient way to handle typical CRUD (Create, Read, Update, Delete) operations for a resource such as a database table.

Generate Resource Controller

To generate resource controller using artisan we should execute the following command

php artisan make:controller ProductController --resource

When you generate a resource controller using Laravel's Artisan command-line tool, it creates a controller class with predefined methods to handle various HTTP verbs and corresponding actions:

This is how generated controller code will look like

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}

By using a resource controller, you can write cleaner and more concise code for handling CRUD operations without having to define each route and controller action individually. Laravel's resource controllers also integrate seamlessly with Laravel's route model binding, making it easy to work with model instances directly in controller methods.

Use Resource Controller in Route

Route::resource('/products', \App\Http\Controllers\ProductController::class);

The line above will register 7 routes. You can inspect these routes if you execute php artisan route:list in terminal. Along with other routes you should see the following output.

Resource Controller Route List

Partial Resource Routes

If you want to exclude certain methods from the resource routes definition you can provide except method after route declaration.

Route::resource('/products', \App\Http\Controllers\ProductController::class)
    ->except(['destroy']);

Or if you want to only include specific routes in the declaration you can provide only method.

Route::resource('/products', \App\Http\Controllers\ProductController::class)
    ->only(['index', 'show']);

API Resource Routes

If you want to define only routes that is relevant for API and exclude create and edit routes, which has the purpose to render HTML form you can use apiResource method instead of resource when defining routes.

Route::apiResource('/products', \App\Http\Controllers\ProductController::class);

If you want to even skip generating create and edit methods in the controller you can provide --api flag to the controller generation.

php artisan make:controller CarController --api

You can also register many API resources at once if you provide array in apiResource method.

Route::apiResource([
    '/products', \App\Http\Controllers\ProductController::class,
    '/cars', \App\Http\Controllers\CarController::class
]);

Discussion

Sign in to join the discussion.

No comments yet. Be the first to start the discussion!