Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

Perhaps like

return Supplier::has('products')->where('name', $name)->get();  

or

return Product::has('suppliers')->where('name', $name)->get();  

This should be what you're looking for- http://laravel.com/docs/4.2/eloquent#querying-relations

Last updated 1 year ago.
0

Don't make it more complicated that you need to.. just do

$supplier = Supplier::with('products')->whereName($name)->first();
return $supplier->products;
Last updated 1 year ago.
0

Thanks for the answers, it works like a charm!

I would like to know how this could have been achieved if the relationship was one-to-many? Say a supplier can have multiple products, but every product belongs to only one supplier..

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

php-it php-it Joined 22 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.