srasch
192
5
Laravel

Route model binding for 2 routes does not work as expected

Posted 8 months ago by srasch

In my routes file I have 2 different 1st level definitions for different models like this (in this order):

Route::get('{category}', 'User\CategoryController')->where('category', '.+')->name('category');
Route::get('{item}','User\ItemController')->where('item', '.+')->name('item');

In my RouteServiceProvider.php I used the concept of explicit model binding (also in this order):

Route::model('item', \App\Item::class);
Route::model('category', \App\Category::class);

Both, the Categorymodel and the Itemmodel have a modified route key name which is just a simple slug in this case.

When opening different category urls in the browser, everything works fine. But on opening the item urls I get a 404page and the debug bar says that Laravel tries to query the Categorymodel.

Hope you guys can help me.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.