I would like to get field value from pivot on many to many relationship in my Laravel 5.4 application.
I have following tables in my MySQL database:
products
+ id
+ name
+ ...
nutrients
+ id
+ name
+ ...
products_nutrients
+ product_id
+ nutrient_id
+ value
products and nutrients tables have its equivalent Eloquent model (Product and Nutrient) with properly working many to many relationship between them.
I need to list all products with values of certain nutrients. I know id of wanted nutrients, so I can set it in query. Result should look like collection/array following of element:
- product name
- value of nutrient id 1 for product
- value of nutrient id 2 for product
- value of nutrient id 3 for product
How can I figure it out using Eloquent query builder or in the worst case in raw SQL query?
I am trying to achieve it but without success. I would be glad of any help.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire