Example 1: laravel join
Inner Join : ->join('contacts', 'users.id', '=', 'contacts.user_id')
Left Join : ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
Right Join : ->rightJoin('posts', 'users.id', '=', 'posts.user_id')
Cross Join : ->crossJoin('colors')
Advance Queries :
-----------------
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
Example 2: laravel not in query
DB::table(..)->select(..)->whereNotIn('book_price', [100,200])->get();
Example 3: join in laravel eloquent
$customer = DB::table('customers')
->join('shops', 'customers.shop_id', '=', 'shops.shop_id')
->where('customer_contact', $contact_no)
->get();
Example 4: laravel join
$latestPosts = DB::table('posts')
->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
->where('is_published', true)
->groupBy('user_id');
$users = DB::table('users')
->joinSub($latestPosts, 'latest_posts', function ($join) {
$join->on('users.id', '=', 'latest_posts.user_id');
})->get();
Example 5: 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 6: 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();