Birendra Gurung
Birendra Gurung

Reputation: 2233

Laravel resource controller method not invoked

I have controllers LeadsController, LeadsAttributesController and LeadsReminderController

Route::resource('leads' , 'LeadsController');
Route::prefix('leads')->group(function(){
    Route::resource('attributes' , 'LeadAttributesController' , ['as' => 'leads']);
    Route::resource('reminders' , 'LeadRemindersController' , ['as' => 'leads']);
});

When I go to /leads/attributes the framework should call the index function, but in this case, a blank page appears and no any exception is shown. However, the route to /leads/attributes/create works as it should.

If the routes are restructured as below, then the routing works fine

Route::prefix('leads')->group(function(){
    Route::resource('attributes' , 'LeadAttributesController' , ['as' => 'leads']);
    Route::resource('reminders' , 'LeadRemindersController' , ['as' => 'leads']);
});
Route::resource('leads' , 'LeadsController');

Can anyone explain this behaviour of the framework?

Upvotes: 0

Views: 473

Answers (1)

Rwd
Rwd

Reputation: 35180

The way to get around this is to put the LeadsController routes under the group routes:

Route::prefix('leads')->group(function(){
    Route::resource('attributes' , 'LeadAttributesController' , ['as' => 'leads']);
    Route::resource('reminders' , 'LeadRemindersController' , ['as' => 'leads']);
});
Route::resource('leads' , 'LeadsController');

The reason you have to do this is because the wildcard for the leads show route will accept anything as default. When laravel receives a request it will try and match it to the first route that it can so since your leads resource is above your nested resources it will match to the show method of leads rather than the correct nested resource.

Upvotes: 1

Related Questions