Search code examples
phplaravelloadingeager

Eager Loading on autogenerated argument in Laravel


I have this route call:

Route::resource('products', 'ProductController', ['except' => ['show']]);

So if I enter /products/438/edit it calls my edit method on controller, that is something like that:

 public function edit(Product $product){}

where $product is, correctly, my SELECT * FROM products WHERE id = 438

now my question is, what if I want to eager load a relation like prices()

I've tried:

$product = $product->with('prices');

and

$product = Product::find($product->id)->with('prices')

but without success. Thanks


Solution

  • You can load relationships to an already existing model or collection using load():

    $product->load('prices')