简体   繁体   中英

Laravel eloquent multiple whereNotIn

I'm trying to make a query where I use multiple whereNotIn arrays. For some reason, they block each other and ignores the other query parameters.

Any clues on how to solve this?

$products = Products::orderBy('id','DESC')
    ->where('status', '=', 4)
    ->whereNotIn('category', $excluded)
    ->whereNotIn('location', ['New York', 'Boston', 'Washington, DC', 'Charlotte'])
    ->take(400)
    ->get();

You need to group condtions orWhereNotIn inside where to exclude both condition, try this

$locations = ['New York', 'Boston', 'Washington, DC', 'Charlotte'];
$products = Products::orderBy('id','DESC')
->where('status', '=', 4)
->where(function($q) use ($locations,$excluded) {
    $q->orWhereNotIn('category', $excluded);
    $q->orWhereNotIn('location', $locations);
})
->take(400)
->get();

Is $excluded an array? If not you can just

->where('category', "<>", $excluded);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM