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

jorgeandresserrano's avatar

Pass parameters to '.index' controller's function

I'm using resourceful routing and I need to pass a parameter to the index function of the controller.

public function index($id){ // do something with $id }

If I try to create a form like (say $user->id = 3):

{{ Form::open(array('route' => array('scopes.index', $user->id))) }} {{ Form::close() }}

I get in the html this link: http://alumni.app/scopes?3 which is not good because it doesn't follow the URI for that named route. So, how should I proceed?

Discussion: I'm trying to load a list of scopes depending on the current user. I made an independent scopes controller.

0 likes
5 replies
foxted's avatar
foxted
Best Answer
Level 8

You could create a seperate route instead:

Route::get('scopes/{id}', ['as' => 'scopes.index', 'uses' => 'ScopeController@index']);

And exclude it from your resource route:

Route::resource('scopes', 'ScopesController', ['except' => ['index']]);
3 likes
jorgeandresserrano's avatar

Hello foxted, thank you very much!

So if I understood correctly I need to create a new controller for that effect.

Can I then create a new function with any name in the same controller and add an independent route (named scopes.index) before registering the resource? I will also exclude the 'index' resource during declaration as you pointed.

Kind of a mix between your answer and this http://stackoverflow.com/questions/16661292/add-new-methods-to-a-resource-controller-in-laravel-4

foxted's avatar

You can create the index function in the same controller, that is not a issue. But usually index methods on resource controllers do not receive any parameters (except maybe query strings).

1 like
jorgeandresserrano's avatar

Great quality answer guys. I wish I could mark both of your answers as correct. They are indeed.

My solution is to go for nested routing, but my question was to pass a parameter to the index function.

Question Solved! ;)

2 likes

Please or to participate in this conversation.