shammon shammon - 5 months ago 21
SQL Question

Laravel query builder: error in mysql where clause

Here is my join query

$data['userslist'] = DB::table('users')
->select('users.*','user_roles.role_name')
->join('user_roles','users.user_type=user_roles.role_id')
->where('users.user_type','!=',1)
->get();


But i will get the following error in where clause


check the manual that corresponds to your MariaDB server version for
the right syntax to use near '
where `users`.`user_type` != ?' at
line 1 (SQL: select `users`.*, `user_roles`.`role_name` from `users`
inner join `user_roles` on `users`.`user_type=user_roles`.`role_id`

where
users
.
user_type
!= 1)

Answer

You try to fetch users.user_type where value in not equal to 1. For this, you need to use '<>' in where clause

Now your query is as follow :

$data['userslist'] = DB::table('users')
        ->select('users.*','user_roles.role_name')
        ->join('user_roles', 'users.user_type', '=', 'user_roles.role_id')
        ->where('users.user_type','<>',1)
        ->get();

I hope you will get your solution.