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();
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?
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();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community