Hello, I'm having some issues with Laravel's query builder, I don't have the same results when I run the query in phpMyAdmin using pure SQL.
Here's my query with Laravel :
$filterResult = DB::table('users')
->where(function ($query) use ($request) {
$string = $request->get('search');
$query->where('name','like','%'.$string.'%')
->orWhere('surname','like','%'.$string.'%')
->orWhere(DB::raw('CONCAT(surname, " ", name)'),'like','%'.$string.'%')
->orWhere(DB::raw('CONCAT(name, " ", surname)'),'like','%'.$string.'%');
})
->whereNotExists(function ($query) use ($request) {
$customer_id = (int)$request->get('customerId');
$query->from('customer_profile_user')
->select(DB::raw(1))
->where('customer_profile_user.customer_id','=',$customer_id)
->where('customer_profile_user.user_id','=','users.id');
})
->whereNotExists(function ($query) use ($request) {
$customer_id = (int)$request->get('customerId');
$query->from('admins')
->select(DB::raw(1))
->where('admins.customer_id','=',$customer_id)
->where('admins.user_id','=','users.id');
})
->whereNotExists(function ($query) {
$query->from('admins')
->select(DB::raw(1))
->where('admins.customer_id','=',1)
->where('admins.user_id','=','users.id');
})
->where('users.id', '!=', Auth::user()->id);
I tried using toSql() to see if the query was good and it seemed to be okay.
Here's the SQL query :
select * from users where
(name like "%ni%" or surname like "%ni%" or CONCAT(surname, " ", name) like "%ni%" or CONCAT(name, " ", surname) like "%ni%")
and not exists (select 1 from customer_profile_user where customer_profile_user.customer_id = 1 and customer_profile_user.user_id = users.id)
and not exists (select 1 from admins where admins.customer_id = 1 and admins.user_id = users.id)
and not exists (select 1 from admins where admins.customer_id = 1 and admins.user_id = users.id)
and users.id != 4
The problem seems to be in the and not exists parts.
Any ideas ?
Thanks.