The issue of not using whereNotExists clause correctly in Laravel 8 Eloquent remains unresolved
P粉287726308
2023-09-05 16:08:35
<p>I have two tables, one is a table with different users and the other is an invoice table called "factures" which has a foreign key userid which I call client_id. What I want to get is the number of customers that were created by a certain admin and don't have an invoice yet. This is the code I tried: </p>
<pre class="brush:php;toolbar:false;">$clients = User::select('id')
->where([['created_by',$membre_id],['role','Client']])
->orWhere([['updated_by',$membre_id],['role','Client']])
->whereNotExists(function($query)
{
$query->select(DB::raw('client_id'))
->from('factures')
->where('created_by',$member_id);
})->get();</pre>
<p>But this query returns me all customers created by $member_id, no exceptions.
Is there anything wrong with my query? </p>
Have you tried the following:
This answer only applies the
OR
condition between the first and second conditions (created_by and updated_by) and its result is combined with theAND
of the third condition .