Loading…

How to use joins in laravel?

Laravel supports various joins that's are given below:-
  • Inner Join

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

  • Left Join / Right Join

    $users = DB::table('admin') ->leftJoin('posts', 'admin.id', '=', 'posts.admin_id') ->get();
    $users = DB::table('admin') ->rightJoin('posts', 'admin.id', '=', 'posts.admin_id') ->get();

  • Cross Join

    $user = DB::table('sizes') ->crossJoin('colours') ->get();

  • Advanced Join

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

  • Sub-Query Joins

    $admin = DB::table('admin') ->joinSub($latestPosts, 'latest_posts', function ($join) { $join->on('admin.id', '=', 'latest_posts.admin_id'); })->get();

Suggest An Answer

No suggestions avaliable!

Latest post from Laravel Interview Questions

Ask Question