Doesn't have through? Hey all, I'm having trouble working out how to get all the products a person hasn't reviewed yet.
I have a User, Product, and Review models. I saw whereDoesntHave but not sure how to use with a "through" relationship.
Review
user_id
product_id
reviewbody
What relationships do you have defined? Do you have a reviews() relationship on your User model, a user() and a product() relationship on your Review model and a reviews() relationship on your Product model?
Does your User model have a products() relationship of the hasManyThrough type, and does your Product model have a users() relationship of the hasManyThrough type?
If you do, shouldn’t this work?
Product::whereDoesntHave(
'users',
fn ($query) => $query->where('user_id', $user->id)
);
@kokoshneta sorry! After your question, I created an ERD to work out the relationships better.
User
HasMany Reviews
Review
BelongsTo User
BelongsTo Product(Type)
BelongsTo Brand
Product(Type)
HasMany Reviews
Brand
HasMany Reviews
Review table
user_id
product_id
brand_id
review
So, what I would be looking for is any product/brand combinations the user doesn't have.
Idea...
Every time a new brand or product type is created, it creates new combos in another table.
Brand
id
name
Product Type
id
name
Product
brand_id
type_id
Review
product_id
user_id
review
Then with user I can do whereDoesntHave... but does that sound like superfluous queries- creating the combos/Product table?
Please sign in or create an account to participate in this conversation.