Tuesday, March 10, 2020

Laravel join query examples

  • nner 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, '

No comments:

Post a Comment