S
S
semki0962019-04-13 17:06:00
Laravel
semki096, 2019-04-13 17:06:00

How to merge 2 collections in Eloquent?

I have 2 tables - t-shirts and color. Many-to-many ratio.
I get the collection of t-shirts in the controller
$products = Product::all();
AND I display the t-shirts in Twig
{% for Product in Products %}
{{ Product.name }}
{% endfor %}
But now how to display the colors? Getting T-shirt colors using a many-to-many relationship
$colors = Product::all()->colors;
Question. As a result, I have two objects - t-shirts and colors. Now, in theory, they should be combined by keys. I have a feeling that I am doing something wrong, this can be done more correctly. I will be grateful for advice.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
semki096, 2019-04-13
@semki096

Found how to do it
$p=Product::with('colors')->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question