Laravel 5.5 Containers

0

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?

  • Method edit() of EnvironmentController.php is called
  • The 1 parameter is passed to the method.
  • The edit (Environment $ environment) method automatically "binds" the ID to the model, and calls it through ORM.
  • 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.

        
    asked by Maramal 04.12.2017 в 14:32
    source

    2 answers

    0

    Change the name of the variable in the controller to $ambiente , in fact, you can see that your parameter has the name of {ambiente} in route:list .

    Greetings,

        
    answered by 31.05.2018 в 00:25
    -1

    Well as your routes indicate, the parameter you receive is ambiente .

    This parameter is the singular of what you put in:

    Route::resource('rutas' , 'MyController'); 
    --> el parametro que recibe mi controlador es ruta
    
        
    answered by 14.12.2017 в 21:13