S
S
Sergey2021-11-09 12:57:39
MySQL
Sergey, 2021-11-09 12:57:39

What is the correct way to get relations through relation 1->3 eloquent?

I have three entities. Many-to-many relationships.

Keyword
Recipe
Category

Relationships between lower entities in tables in the database.
category_keyword
keyword_recipe

To make the category_recipe link I did the following

$category = Category::with(['keywords'])->where('code', $code)->first();
        $recipes = collect();
        foreach ($category->keywords as $keyword){
            foreach ($keyword->recipes as $recipe){
                $recipes->push($recipe);
            }
        }

But something tells me that this is not entirely true :). In the template (View), iterating foreach through the third knee is also not entirely correct. Especially if you need to set certain conditions.

Tell me how to properly build a request through Eloquet in my case, the category_recipe relation

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2021-11-09
@jazzus

hasManyThrough https://laravel.com/docs/8.x/eloquent-relationship...
or Recipe whereHas Keyword whereHas Category

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question