Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yougotnet's avatar

Route::resource add parameters

Is it possible to add/pass parameters to a Route::resource('users', UserController::class)?

I have a variable that I want passed to all routes.

0 likes
2 replies
LaryAI's avatar
Level 58

Yes, you can pass parameters to a Route::resource by using route model binding or by defining additional routes that include the parameters you need. However, if you want to pass a parameter to all routes within a resource controller, you might need to define the routes manually or use a route group with a parameter.

Here's an example of how you can achieve this by using a route group with a parameter:

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

Route::prefix('users/{parameter}')->group(function () {
    Route::get('/', [UserController::class, 'index'])->name('users.index');
    Route::get('/create', [UserController::class, 'create'])->name('users.create');
    Route::post('/', [UserController::class, 'store'])->name('users.store');
    Route::get('/{user}', [UserController::class, 'show'])->name('users.show');
    Route::get('/{user}/edit', [UserController::class, 'edit'])->name('users.edit');
    Route::put('/{user}', [UserController::class, 'update'])->name('users.update');
    Route::delete('/{user}', [UserController::class, 'destroy'])->name('users.destroy');
});

In this example, {parameter} is a placeholder for the parameter you want to pass to all routes. You can access this parameter in your controller methods by using Laravel's dependency injection or by retrieving it from the route parameters.

For example, in your UserController, you can access the parameter like this:

public function index($parameter)
{
    // Use $parameter as needed
}

This approach allows you to pass a parameter to all routes within the resource controller.

yougotnet's avatar

I should of been more specific; I want to an object to the route. I have a ton of route that has to lookup a particular record and I want to only do it once for all the routes.

Please or to participate in this conversation.