Q
Q
Qweqwe12342015-07-31 15:54:13
Laravel
Qweqwe1234, 2015-07-31 15:54:13

How to create a 1 to many relationship across 2 tables?

Good afternoon, is it possible and how to organize a 1-to-many relationship through 2 intermediate tables using Active Record on Laravel 5.1?
For more clarity, using an example from the documentation: laravel.com/docs/5.1/eloquent-relationships#has-ma...
There are country tables, users, posts. We want to easily get all the posts belonging to a certain country - everything is great. Add another table - comments to posts. Now I would like to receive comments belonging to posts of a certain country (that is, get comments through the posts table with which we have a connection through the users table), is it possible to implement such a connection 'through through' or what is the best way to do it? We need comments at least in order to assess how actively the posts belonging to a certain country are discussed, that is, for example, to get the number of comments on posts created by users of a certain country.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Plisko, 2015-07-31
@AmdY

It would be nice to see the structure of the tables, otherwise it’s not clear what the complexity is and why there are some strange connections

// комментарии принадлежащие постам определенной страны
Comments::whereHas('posts', function ($query) use($country) {
    $query->where('country', $country);
})->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question