1 year ago

Flipping Model->Relationship Order

Posted 1 year ago by cabeeb

I'm building an app with Players that can be invited to Games. Players can also be invited to other things, so I created a polymorphic Invite model to store the invite response, time, etc.

public function player()
    return $this->belongsTo('App\Player');

public function invitable()
    return $this->morphTo();

I need to be able to get all of the players that have been invited to a particular game, as well as their invite status. The best I can come up with is to do $game->load('invites.players') which gives me:

App\Invite {
    id: 1,
    player: App\Player {

But I'd really like for the Player to be the top-level model, with an invite attribute, almost like a pivot. I was able to do it with this mess, but it feels really gross:


$invitedPlayers = $game->invites->map(function($invite){
    $obj = $invite->player;
    $obj->invite = $invite;
    return $obj;

Any advice?

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