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

RushVan's avatar

Can I use lists method with a join?

I have the following join;

    $players = PlayedGame::join('players', 'played_games.playerId', '=', 'players.playerId')
        ->where('played_games.game', '=', $request->id)->get();

in my Player model I have created this gettor;

   public function getFullPlayerAttribute()
{
    return $this->firstName . ' ' . $this->lastName . ' ' . $this->number;
}

Now I would like to use the attribute with the lists method in my controller like so;

 $players = PlayedGame::join('players', 'played_games.playerId', '=', 'players.playerId')
        ->where('played_games.game', '=', $request->id)->get()->lists('full_player', 'playerId');

But that returns this;

Collection {#345 ▼
  #items: array:40 [▼
    43 => null
    44 => null
    45 => null
    46 => null
    47 => null...

Null instead of my full_player attribute. Is there something I am missing?

Thanks!

0 likes
1 reply
RushVan's avatar
RushVan
OP
Best Answer
Level 5

Fudge! Best part about this forum (beyond all the killer help...) is you need to really think about your question in order to post it. And then, you usually solve it yourself!

Turns out I should have been calling the Player model first not the PlayedGame.

This works;

        $players = Player::join('played_games', 'players.playerId', '=', 'played_games.playerId')
        ->where('played_games.game', '=', $request->id)->get()->lists('full_player', 'playerId');
    dd($players);

Thanks if you even got to reading this...

Please or to participate in this conversation.