PenAndPapers PenAndPapers - 1 year ago 97
PHP Question

Laravel concat not working

I have an issue on Laravel CONCAT it doesn't concatenate DB fields. When I checked the returned data it has no fullName

public function getAllPlayers()
{
$data = Player::select(DB::raw('CONCAT(familyName," ",firstName) AS fullName'))
->orderBy('familyName', 'asc')
->join('teams', 'players.primaryClubId', '=', 'teams.clubId')
->select(['players.*', 'teams.teamName', 'teams.teamNickname', 'teams.teamCode'])
->get()
->unique() //remove duplicates
->groupBy(function($item, $key) { //group familyName that starts in same letter
return substr($item['familyName'], 0, 1);
})
->map(function ($subCollection) {
return $subCollection->chunk(4); // put your group size
});

return $data;
}

Answer Source

You are overriding select with another select. To add multiple select you need to use addSelect method.

public function getAllPlayers()
{
    $data = Player::select(DB::raw('CONCAT(familyName," ",firstName) AS fullName'))
        ->orderBy('familyName', 'asc')
        ->join('teams', 'players.primaryClubId', '=', 'teams.clubId')
        ->addSelect(['players.*', 'teams.teamName', 'teams.teamNickname', 'teams.teamCode'])
        ->get()
        ->unique() //remove duplicates
        ->groupBy(function($item, $key) { //group familyName that starts in same letter
            return substr($item['familyName'], 0, 1);
        })
        ->map(function ($subCollection) {
            return $subCollection->chunk(4); // put your group size
        });

    return $data;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download