Reputation: 1807
I have 3 model: Course,group,student
I want to give games from Course with 2 relations.
for ex: I want students of Course 1 (id=1)
The Course has Many group(5,6,8) and each group have One(36,38) or Many(35,37) students
How to get all students of Course with relations and eloquent
Upvotes: 0
Views: 3554
Reputation: 2683
If you need only games:
$games = Game::with(['some_relation_name', 'some_relation_name_2'])
->whereHas('group', function($query) {
$query->whereHas('tournament', function($query) {
$query->where('id', 1)
});
})
->get();
If you need tournament with games, Anar's option is better.
Upvotes: 1
Reputation: 11594
You can use hasManyThrough
public function games()
{
return $this->hasManyThrough(Tournament::class, Group::class);
}
The "has-many-through" relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. For example, a Country model might have many Post models through an intermediate User model. In this example, you could easily gather all blog posts for a given country.
https://laravel.com/docs/5.7/eloquent-relationships#has-many-through
Upvotes: 2