jeudi 9 juin 2022

How to use laravel group concat

I have a pivot table for property, client and agent

...

public static function client_property_list($id)
{
    $client_property_list = DB::table('agent_client_property as acp')
        ->leftJoin('clients as c', 'acp.client_id', '=', 'c.id')
        ->leftJoin('properties as p', 'p.id', '=', 'acp.property_id')
        ->leftJoin('agents as a', 'a.id', '=', 'acp.agent_id')
        ->select(DB::raw('p.*'))
        ->where('c.id','=',$id)
        ->get();

    return $client_property_list;
} 

...

one property can have multiple agents, i want to group those agents to show only 1 on the list

tried this didnt worked

public static function client_property_list($id)
    {
        $client_property_list = DB::table('agent_client_property as acp')
            ->leftJoin('clients as c', 'acp.client_id', '=', 'c.id')
            ->leftJoin('properties as p', 'p.id', '=', 'acp.property_id')
            ->leftJoin('agents as a', 'a.id', '=', 'acp.agent_id')
            ->select(DB::raw('p.*,a.first_name as name'))('group_concat(name) as names')
            ->where('c.id','=',$id)
            ->whereNull('c.deleted_at')
            ->whereNull('p.deleted_at')
            ->whereNull('a.deleted_at')
            ->get();

        return $client_property_list;
    } 


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire