Eager loading with route model binding
We can eager load the needed relations by customizing the resolution logic (for route model binding) by defining a resolveRouteBinding
method on the model.
// In the Novel model
public function resolveRouteBinding($value)
{
return $this->with(['chapters', 'bookmarks'])->where($this->getRouteKeyName(), $value)->firstOrFail();
}
https://laravel.com/docs/6.x/routing#explicit-binding
There is “Lazy Eager Loading“.
The syntax is $novel->load('chapters', 'bookmarks');