Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

You don't have a relationship directly to products from users, but you should be able to nest them.

Maybe,

User::find(1)->with('purchases', 'purchases.product')->where('product.price', 79)->first();
0

The first part works great to retrieve the product, thanks!

User::find(1)->with('purchases', 'purchases.product')

The condition, however fails, and so I will be looking further into how to query on the returned 'product' JSON in order to deduce the price of the product.

->where('product.price', 79)->first();

Based upon your mention of the lack of relationship between users and products, another thought came up. A user can have many different products, and a product can have many different users. So would it make sense to establish a many to many relationship between users and products with an intermediate pivot table?

0

Try this,

// split the with statements
// a where following a with should be part of the with
User::find(1)->with('purchases')->('purchases.product')->where('price', 79)->first();

Let me think on the relationships,

User -> Purchases -> Products
Products -> Purchases -> Users

I'm not sure about, Users -> Products, you should be able to get the products for a user through the purchases

// pull only the product relationship through the purchases
User::find(1)->('purchases.product')->where('price', 79)->first();
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

gamzarme gamzarme Joined 9 Feb 2016

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.