V
V
Vitaly2018-05-22 09:13:16
Laravel
Vitaly, 2018-05-22 09:13:16

How to make a request to the database?

There is a table of products. This table has many-to-many relationships with 3 other tables "color", "weight", "growth".
Schematically:

produkts <-> products_color    <-> color
               <-> products_weight <-> weight
               <-> products_growth <-> growth

How to write a query to get something like this array as an output:
produkts 1 => [
                     name => produkts 1,
                     slug => produkts 1,
                     color => [
                                  [0] => red
                                  [1] =>blak
                     ],
                     weight => [
                                  [0] => 100
                                  [1] => 200
                     ],
                     growth => [
                                  [0] => 150
                                  [1] => 250
                     ],
],
produkts 2 => [
                     name => produkts 2,
                     slug => produkts 2,
                     color => [
                                  [0] => red
                     ],
                     weight => [
                                  [0] => 100
                     ],
                     growth => [
                     ],
].....

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iljaGolubev, 2018-05-22
@kiukishenkaec

https://laravel.com/docs/5.6/eloquent-relationship...
https://laravel.com/docs/5.6/eloquent-relationship...

$produkts = App\Produkts::with(['color', 'weight', 'growth'])->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question