Laravel Routing

Register 1:1 Resources with Route::singleton()

Punyapal Shah 1 min read
edit this tip
Use Route::singleton() to register RESTful routes for resources that have only a single instance per user (like /profile or /settings).

Standard Route::resource() registers plural routes with identifier parameters (e.g. /users/{user}). For singleton resources where only one record exists for the authenticated user (such as /profile or /settings), passing IDs in the URL is unnecessary.

Route::singleton() registers dedicated singular routes without ID parameters.

Registering a Singleton Resource

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

// Registers: GET /profile, GET /profile/edit, PUT /profile
Route::singleton('profile', ProfileController::class);

Generated Routes

VerbURIActionRoute Name
GET/profileshowprofile.show
GET/profile/editeditprofile.edit
PUT/PATCH/profileupdateprofile.update

Nested Singletons

Singletons can also be nested under parent resources:

// Registers: /photos/{photo}/thumbnail
Route::resource('photos', PhotoController::class);
Route::singleton('photos.thumbnail', ThumbnailController::class);

Summary

  • Eliminates redundant {id} parameters for single-instance resources.
  • Maps to standard show, edit, and update controller methods.
  • Supports nested resource relationships.
Tags: Laravel Routing REST Clean Code
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips