The issue of not using whereNotExists clause correctly in Laravel 8 Eloquent remains unresolved
P粉287726308
P粉287726308 2023-09-05 16:08:35
0
1
634
<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>
P粉287726308
P粉287726308

reply all(1)
P粉670838735

Have you tried the following:

$clients = User::select('id')
    ->where(function($query) use($member_id){
        $query->where([['created_by',$membre_id],['role','Client']])
            ->orWhere([['updated_by',$membre_id],['role','Client']])
    })
    ->whereNotExists(function($query) use($member_id){
        $query->select(DB::raw('client_id'))
            ->from('factures')
            ->where('created_by',$member_id);
    })
    ->get();
}

This answer only applies the OR condition between the first and second conditions (created_by and updated_by) and its result is combined with the AND of the third condition .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template