Manjunath Manjunath - 1 month ago 9
MySQL Question

How can I write Left Join Clause

I have MySQL query like below

SELECT AVG(COALESCE(ratings.rating_for_manager, 0)), consultants.id
FROM consultants
LEFT JOIN ratings ON ratings.consultant_id = consultants.id
GROUP BY consultants.id


How can I write above MySQL join query in laravel Left Join Clause?

Answer Source

You can use leftJoin:

DB::table('consultants')
    ->selectRaw('AVG(COALESCE(ratings.rating_for_manager, 0)), consultants.id ')
    ->leftJoin('ratings', 'ratings.consultant_id', 'consultants.id')
    ->groupBy('consultants.id')
    ->get();

https://laravel.com/docs/5.4/queries#joins

Hope this helps!