Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GreenGo's avatar

Retrive deep relation

Hi, I have three tables in my DB. Tournaments, Games and Plays. One Tournament have multiple Games, one Game belongs to one Tournament, one Game have multiple Plays, and one Play belongs to one Game. In store tournament_id in Game table and game_id in Plays table. I try sth like this,

Tournament::find(1)->games->plays 

but it doesnt worked. Relation hasManyThrough() is not solution for me either.

Which way is the best to get tournament with games an plays ?

0 likes
4 replies
GreenGo's avatar

Hmm, this returns only tournaments that contain a plays but I still do not have access to the data :/ Or I`m doing something wrong ?

$tournament = Tournament::where('slug', $slug)->has('games.plays')->first();

dd($tournament->games->plays); //error
dd($tournament->plays); //null

This is wat I want to display:

-tournament 
---game1
------play
------play
---game2
------play
------play      
...
Snapey's avatar
Snapey
Best Answer
Level 122

Eager loading (with)

$tournament = Tournament::with('games.plays')->find(1);

then, eg in blade view

@foreach($tournament->games as $game)

    <h2>{{ $game->title }}</h2>  // or name or whatever
    // details about game

    @foreach($game->plays as $play)

        <h3>{{ $play->title }}</h3>  //or name or whatever
        //details of play

    @endforeach 
@endforeach 

1 like

Please or to participate in this conversation.