3

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.

1
  • Route::resource's index route doesn't take a parameter. Commented Dec 28, 2014 at 5:06

2 Answers 2

4

The index action of a resource controller / route doesn't take any parameters by design.

For filtering I suggest you use query parameters.

/scope?userId=1

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

Another approach would be nested resources (scroll down a bit)

If you nest the scope resource inside user you could get this kind of url:

/user/1/scope
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @lukasgeiter I'll be nesting the routes for a better design.
1

You could create a separate 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']]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.