Home > Software engineering >  how should I use in one where clause check one field by more than one value
how should I use in one where clause check one field by more than one value

Time:06-21

          where("mime_type", "not like", "image%")
        ->where("mime_type", "not like", "video%")
        ->where("mime_type", "not like", "audio%")
        ->where("mime_type", "not like", "application%")

how should use all of them in one where() in laravel eloquent model

CodePudding user response:

You'll have to prepare the data to be excluded before running the ->where() method:

$toExclude = ['image', 'video', 'audio', 'application'];
        
$excludedValues = array_map(
    callback: fn($value) => ['mime_type', 'not like', $value . '%'],
    array: $toExclude
);

$images = Image::query()->where($excludedValues)->get();

or, without using array_map:

$toExclude = ['image', 'video', 'audio', 'application'];
        
$excludedValues = [];
        
foreach ($toExclude as $value) {
    $excludedValues[] = ['mime_type', 'not like', $value . '%'];
}
        
$images = Image::query()->where($excludedValues)->get();

CodePudding user response:

You can use whereNotIn method.

Like:

$admins = \App\Models\Users::whereNotIn('role', ['customer','employee','vendor'])->get();

So You get results of users who are not customer,vendor, employee.

CodePudding user response:

Use Laravel's orWhere method.

 ->where("mime_type", "not like", "image%")
 ->orWhere("mime_type", "not like", "video%")

For more detail please refer document https://laravel.com/docs/9.x/queries#where-not-clauses

  • Related