Ours Ours - 1 month ago 12
PHP Question

Laravel routing auto-resolve parameters

This might be a beginner question, but there is something I don't quite understand in my Laravel application.
I'm using id on routes and I'm trying to make laravel automatically fetching the correspondant objects in the DB. It works well for some models :
In the routing :

Route::get('/project/{project}', 'ProjectController@useProject');


and in the controller

public function useProject(Request $request, Project $project)


and after that I have my "project" fetched from the DB

protected 'attributes' =>
array (size=2)
'id' => int 9
'name' => string 'test' (length=4)
...
public 'exists' => boolean true


In another part of the code, where I'm using the same routing structure on a different model, the auto-resolve doesn't seem to happen

Routing:

Route::get('/build/{testscenario}','JavaController@create');


Controller:

public function create(Request $request, TestScenario $scenario){


And the result:

protected 'attributes' =>
array (size=0)
empty
...
public 'exists' => boolean false


Both models are using a "id" column as a primary key.
What am I missing ?

Thanks a lot for the help!

Answer

You are trying to use the model implicit binding, so I would recommend reading this section from the documentation.

You need to match the variable names:

Route::get('/build/{testscenario}','JavaController@create');

And in your controller:

public function create(Request $request, TestScenario $testscenario){