nexxai
1 year ago

How do I properly sort this distant relationship

Posted 1 year ago by nexxai

Here's the code:

    $performer = Performer::with(
        'castmember.performance.tour.production',
        'castmember.character'
    )
        ->where('id', $id)
        ->firstOrFail();

Here are the relationships:

Performer:

public function castmember()
{
    return $this->hasMany(\App\Castmember::class);
}

Castmember:

public function performance()
{
    return $this->belongsTo(\App\Performance::class);
}

and I'm trying to sort by the "date" property on the Performance model so that we can display a chronological list of every performance a performer has been a part of.

The Castmember model is something like a pivot table except each instance tracks 3 things: the performer we're talking about, the performance they performed in, and the character they played.

What I've tried:

  • Various joins
  • Query scopes
  • Nested query constraints
  • probably some other stuff I'm forgetting here

I feel like this can't be the first time someone's tried to do this, but I'm at a loss here; what am I missing?

Please sign in or create an account to participate in this conversation.