In Laravel 5, when you create a model with resource controller and migration ( php artisan make:model Environment -mr
) you can notice that the methods are automatically generated in the controller.
Now, if you look at each method created automatically by Artisan in the controller, in some cases, it has a single model type parameter, such as: public function edit(Environment $environment) {}
In the routes file in routes / web.php I have defined the resource as Laravel advised (I think):
Route::resource('ambientes', 'EnvironmentController');
Now, if I enter / ambientes / 1 / edit , I should do the following process, right?
edit()
of EnvironmentController.php is called
Well, all that does not happen. Instead, it returns an empty App\Environment
object.
The only way I can achieve what I'm trying to do is manually create the routes in the following way:
Route::get('ambientes', 'EnvironmentController@index');
Route::get('ambientes/create', 'EnvironmentController@create');
Route::post('ambientes', 'EnvironmentController@store');
Route::get('ambientes/{environment}/edit', 'EnvironmentController@edit', function(App\Environment $environment) {});
Route::post('ambientes/{environment}', 'EnvironmentController@update', function(App\Environment $environment) {});
Route::post('ambientes/{environment}/delete', 'EnvironmentController@destroy', function(App\Environment $environment) {});
In fact, if I try to modify the methods in the controller to receive both parameters I am only able to access the $ id :
public function edit ($ id, Environment $ environment) {}
According to the route: list command:
Well, how should I proceed to achieve the injection of the model as the only parameter using only the Route::resource('ambientes', 'EnvironmentController');
?
I appreciate any help.