I have 3 tables – users, products and user_products. I try get user_products for user with products data. I can get user_products by $this->hasMany(ProductUser::class)
but how can I get products atributes (title and other) from Product table in one query? Or I have to do query for every product? I’m new in Laravel, tnx.
2
Answers
That sounds like a many-to-many relationship between users and products to me. I suggest set up a BelongsToMany relationship between theses two:
And you can always retrieve data from the pivot table, in this scenario, that
user_products
table.In User Modal -> User.php
In Product Modal -> Product.php
In your controller -> Eger load the product with user
In your blade file to show data -> user/ products.blade.php