skip to Main Content

I am trying to select some data from two tables, here is sql code

`SELECT users.name FROM users, friends WHERE super_admin= false and friends.friend_id=users.id     and friends.user_id = 4 and friends.status= 1`

I used sql to eloquent converter and it returns

`DB::table("users, friends")
->select("users.name")
->where("super_admin", "=", false)
->where("friends.friend_id", "=", users.id)
->where("friends.user_id", "=", 4)
->where("friends.status", "=", 1)
->get();`

but the problem is on the first line it seems that its impossible to write DB::table("users, friends") it was probably possible in some old versions of laravel.

2

Answers


  1. You can use DB::raw:

    DB::table(DB::raw("users, friends"))
    ->select("users.name")
    ->where("super_admin", "=", false)
    ->where("friends.friend_id", "=", users.id)
    ->where("friends.user_id", "=", 4)
    ->where("friends.status", "=", 1)
    ->get();
    
    Login or Signup to reply.
  2. Use Join statement to join the 2 tables

    DB::table('users')
    ->join('friends', 'friends.friend_id', '=', 'users.id')
    ->select('users.name')
    ->where('super_admin', false)
    ->where('friends.user_id', 4)
    ->where('friends.status',  1)
    ->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search