join count laravel code example

Example 1: join table laravel count

DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.id', 'contacts.phone', 'orders.price')
            ->get();

Example 2: count with left join in laravel

$areas = Area::select('areas.*', DB::raw('count(area_id) as connections'))
	->leftJoin('object_areas', 'object_areas.area_id', '=', 'areas.id')
	->groupBy('areas.id')
	->get();

Example 3: join table laravel count

DB::table('users')
        ->join('contacts', function($join)
        {
            $join->on('users.id', '=', 'contacts.user_id')->orOn(...);
        })
        ->get();

Example 4: join table laravel count

DB::table('users')
        ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
        ->get();

Tags:

Php Example