Laravel — P59: Partial Resource Routes

Register only what you need with partial resource routes

If you haven’t figured out Laravel’s flexibility just yet, I believe this article will definitely solidify it. Not only did the Laravel developers think of most common concepts, they’ve also though about exceptions to those concepts.

For example, in the previous article we walked through resource controllers and we finished with creating all of the routes with a single line of code.

use App\Http\Controllers\CameraController;

Route::resource('cameras', CameraController::class);
But what if we didn’t want to utilize the resource route creation technique for all of our routes, but we wanted to utilize its elegance for some of the routes? That concept exists. Do we haver to use that concept? Of course not. We could simply list out all of our routes.
use App\Http\Controllers\CameraController;

Route::prefix('/camera')->group(function() {
    Route::get('/',          [CameraController::class, 'index']);
    Route::get('/create',    [CameraController::class, 'create']);
    Route::post('/',         [CameraController::class, 'store']);
    Route::get('/{id}',      [CameraController::class, 'show']);
    Route::get('/{id}/edit', [CameraController::class, 'edit']);
    Route::put('/{id}',      [CameraController::class, 'update']);
    Route::delete('/{id}',   [CameraController::class, 'destroy']);
});

So how do we utilize the resource controller for only some of the routes? There are two different ways:

  • only
  • except

The only method

When we want to utilize the resource controller for only a couple of resources, we can chain on the only method to our resource route and specify which routes we want to apply the resource routes for.

Route::resource('cameras', CameraController::class)->only([
    'index', 'show'
]);

We can then specify the other routes however we want. If we only had those two to begin with (i.e. we didn’t allow insertions, edits, nor deletions), this would generate those routes and not the other ones.

The except method

When we want to utilize the resource routes for the majority of the routes, we could utilize the except method to exclude the routes that we don’t want Laravel to generate. For example, let’s say that we don’t allow deletions, but we do want routes for everything else.

Route::resource('cameras', CameraController::class)->except([
    'destroy'
]);
This line generates all of the routes for us except the route for destroy.

Laravel Series

Continue your Laravel Learning.

Laravel — P58: Resource Controllers

Simplify CRUD with resource controllers

Laravel – P58: Resource Controllers

In part 58 of our Laravel fundamentals series, learn how resource controllers streamline CRUD operations. Explore generator commands, automatic RESTful routes, customizing actions, and keeping your controllers clean and maintainable. (233 characters)

Laravel — P59: Partial Resource Routes

Register only what you need with partial resource routes

Laravel – P59: Partial Resource Routes

In part 59 of our Laravel fundamentals series, discover how partial resource routes let you register only the endpoints you need. Learn the only and except options, group route patterns, and keep your routing file lean and secure.

Laravel — P60: Resource Route Names

Name routes your way

Laravel – P60: Resource Route Names

Part 60 of our Laravel fundamentals series unpacks how to customize resource route names for maximum clarity. Override default naming, prevent helper collisions, and keep large-scale projects organized with readable, intuitive route helpers and API endpoints.

Leave a Reply