Laravel route model binding for just one route

I have a controller with 8 methods. Seven of them use this statement:

$item = Item::findOrFail($id);

However, only one needs a different query:

$item = Item::with('subitem')->findOrFail($id);

I would like to define a route model binding for the first seven of these methods, however, is there a way to somehow tell the Laravel that for the last method I want the ID injected instead of an instance of the Item class? Currently I’m doing the following but its quite bad:

$item = Item::with('subitem')->findOrFail($item->id);

Source: laravel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.