mercredi 1 juin 2016

Laravel - Using a nested relationship with the query builder

I am working on an API but its starting to get a bit slow now that the data is increasing. I am moving some of the queries so that they use the DB query builder.

I have my last one which has a nested query:

$artists = Artist::with('performances', 'performances.stage')->get();

I have got so far:

$artists = \DB::table('artists')
    ->leftJoin('performances', 'artists.id', '=', 'performances.artist_id')
    ->get();

But now need to do the second relationship which in the Performance model is:

public function stage()
{
    return $this->hasOne('App\Models\Stage', 'id', 'stage_id');
}

Any help on how I do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire