Query clause with two whereIn

I try to run this query , all is good till :

->whereIn(‘statut_licence_id’, [‘1’ , ‘4’])

this second whereIn seems to not work , it’s display me only statut_licence_id = 1 and not also 4 , someone now how to resolve the problem ?

        $licencies = Licencies::whereIn('structure_id', $structures->pluck('id'))->whereIn('type_licence_id', ['1', '2', '3', '4', '5'])
            ->whereIn('statut_licence_id', ['1' , '4'])
            ->where('valid_licence_id', '1')
            ->where('lb_assurance_etat' ,'=' , 'Assurée')
            ->orderBy('created_at', 'DESC')
            ->paginate(10);

problem solved with :
$licencies = Licencies::whereIn('structure_id', $structures->pluck('id'))->whereIn('type_licence_id', ['1', '2', '3', '4', '5']) ->where(function ($query) {
            $query->whereIn('statut_licence_id', ['1' , '4'])
            ->where('valid_licence_id', '1'); })
            ->orderBy('created_at', 'DESC')
            ->paginate(10);

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

try to use closure.

$licencies = Licencies::whereIn('structure_id', $structures->pluck('id'))->whereIn(function ($query){
             $query->whereIn('type_licence_id', ['1', '2', '3', '4', '5'])
            ->whereIn('statut_licence_id', ['1' , '4']);
        })
        ->where('valid_licence_id', '1')
        ->where('lb_assurance_etat' ,'=' , 'Assurée')
        ->orderBy('created_at', 'DESC')
        ->paginate(10);

Method 2

You can follow these steps to check your query statement:

Firstly, before the statement:

DB::enableQueryLog();

secondly, use dd to print your sql after the statement:
dd(DB::getQueryLog());

finally, copy and paste the sql in you database client, you’ll get what you want. And you don’t need ask anyone with the similar again.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x