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

Classified's avatar

Route parameter does not get passed to controller

Hello,

I am trying to pass a route parameter to a controller and for some reason it is omitted from the request object. The input fields from the form are passed to the controller with no issue, but the route parameter itself is the only thing that is omitted.

Route code

Route::post('/item/{item}/test', [
    'uses' => 'ItemController@test',
    'as' => 'item.test',
])->where('item', '[0-9]+');
protected function test() {
    return request()->item; // returns null
}

The ID of the item is visible within the link.

0 likes
7 replies
Yorki's avatar
Yorki
Best Answer
Level 11
public function test(Request $request, $item) 
{
    dd($item);
}
2 likes
Classified's avatar

That fixes the issue, but how come it is omitted from the request object? It works on all of my other controllers, that are written the exact same.

Mithrandir's avatar

There must be some kind of difference to the other controllers, but without seeing them, it's hard to tell.

Generally, the @Yorki way of passing URL parameters to the controller method is "the right one"(tm)

1 like
Sirik's avatar

Try this

Route::post(
     '/item/{item}/test',
     'ItemController@test'
)
->name('item.test');

And in your Item controller

public function test(Request $request, Item $item) 
{
    dd($item);
}
Classified's avatar

@Mithrandir That is why it's weird, I feel like I've looked over this several times and by eye there is no difference. The model gets resolved via the RouteServiceProvider, and then gets passed around the views like it should, but as soon as it hits this exact route it becomes an issue. On all my other controllers I use request()->item to retrieve the item.

I am aware that @Yorki way is the right one, just trying to figure out why this doesn't work.

jlrdw's avatar

A query string has the passed request objects built in, a passed parameter does not.

It is a HTTP standard.

1 like
Classified's avatar

Well let's take this example

Controller

protected function store() {
    $meta = request()->meta;
    
    return $meta // This returns the meta object.
}

Route

Route::post('/{meta}', [
    'uses' => 'MetaController@store',
    'as' => 'meta.store',
])->where('meta', '[0-9]+');

It is the exact same in setup as my other example, however the main difference is that this example actually works.

Could there be ANY reason why this would work and the other not working?

Note: The only real difference in these two examples is the fact that their RouteServiceProvider binds are going to two seperate models respectively.

also, while using the 'right way' of passing the route parameter. How would I go about specific validation as you would do with the validate() helper

Please or to participate in this conversation.