A
A
andre77772016-12-05 18:58:31
Laravel
andre7777, 2016-12-05 18:58:31

How to make a whereIn query if the array is empty?

have a request

Rosid::
            whereIn('user_id',$users_id)
            ->WhereIn('status_id',$statuses)
            ->get();

if the arrays are not empty - everything works.
If one of the arrays is empty, it returns nothing.
whereIn can be greater than two.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Aksentiev, 2016-12-05
@andre7777

$query = new Rosid();

if(count($users_id)){
    $query = $query->whereIn('user_id',$users_id);
}

if(count($statuses)){
    $query = $query->WhereIn('status_id',$statuses);
}

$query->get();

A
Alexey, 2016-12-05
@rdifb0

Put zeros in there. To not be empty.

V
Vlad, 2016-12-05
@Result007

Try one of the options:
1) whereIn('user_id',$users_id)->whereIn('status_id', $statuses, 'or')->get();
2) whereIn('user_id',$users_id)->orWhereIn('status_id', $statuses)->get();
This is for the second whereIn, so you can for anyone :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question