arthur arthur - 1 year ago 167
PHP Question

Laravel RESTful controller add parameter to create

I am trying to use Laravel 5.2's RESTful resource controllers. However, when moving from my index to create, I would like to pass a parameter as the create page should be partially filled in.

The form that will be 'created' will have populated fields from the database already. So the create should take the id from the user that is clicked in the index.

My temporary solution:

Route::get('consultation/{id}', 'ConsultationController@create');
Route::resource('consultation', 'ConsultationController', ['except' => ['create']]);

Is there a way to add this to an options array in the same line as resource?


Edit: I suppose in this case, my store would also need the same {id} parameter.

Answer Source

I was also curious about that but I think that the Laravel Documentation is pretty clear about this:

If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource

And they add the following, meaning for me that if you want to override a defined route, you just need to put the definition on top of the Route::resource() definition.

otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes


After better understanding the question, I would let the restful controller as is, and create a new route like /user/{user-id}/consultations/create that is much more "restful" like.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download