我应该如何在子句中使用一个以上的一个值来检查一个字段

发布于 2025-02-09 07:13:17 字数 278 浏览 3 评论 0原文

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

如何将所有这些都在laravel雄伟模型中的()中使用

          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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不及他 2025-02-16 07:13:17

在运行- > where()方法:

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

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

或,不使用array_map

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

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();
好菇凉咱不稀罕他 2025-02-16 07:13:17

您可以使用Wherenotin方法。

喜欢:

$ admins = \ app \ models \ users :: wherenotin('角色',['customer','雇员','vendor']) - > get();

,以便您得到不是客户供应商员工的用户的结果。

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.

海螺姑娘 2025-02-16 07:13:17

使用Laravel的或Where方法。

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

有关更多详细信息,请参阅文档 https://laravel.com/docs/ 9.x/查询#where-not-clauses

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

柒七 2025-02-16 07:13:17

尝试这个。将数组传递到Where子句中。

$exclude_values = [
        ['mime_type' ,'not like','image'.'%'],
        ['mime_type' ,'not like','video'.'%'],
        ['mime_type' ,'not like','audio'.'%'],
        ['mime_type' ,'not like','application'.'%'],
    ];
    $wherein_query = \App\Models\ModelName::where($exclude_values)
                ->get();

希望这对您有用。

Try this one. Pass array in where clause.

$exclude_values = [
        ['mime_type' ,'not like','image'.'%'],
        ['mime_type' ,'not like','video'.'%'],
        ['mime_type' ,'not like','audio'.'%'],
        ['mime_type' ,'not like','application'.'%'],
    ];
    $wherein_query = \App\Models\ModelName::where($exclude_values)
                ->get();

Hope this will work for you.

秉烛思 2025-02-16 07:13:17

我有简单的方法来解决这个问题:
- > where()之类的Where函数中制作功能:

$your_model->where(function ($query) {
  $query->where("mime_type", "not like", "image%")
        ->where("mime_type", "not like", "video%")
        ->where("mime_type", "not like", "audio%")
        ->where("mime_type", "not like", "application%");
})

它将生成类似:

where
  (
    `mime_type` not like 'image%'
    and `mime_type` not like 'video%'
    and `mime_type` not like 'audio%'
    and `mime_type` not like 'application%'
  )

希望此帮助。

I have simple way to resolve this:
Make the where function inside ->where() like this:

$your_model->where(function ($query) {
  $query->where("mime_type", "not like", "image%")
        ->where("mime_type", "not like", "video%")
        ->where("mime_type", "not like", "audio%")
        ->where("mime_type", "not like", "application%");
})

It will generate like:

where
  (
    `mime_type` not like 'image%'
    and `mime_type` not like 'video%'
    and `mime_type` not like 'audio%'
    and `mime_type` not like 'application%'
  )

Hope this help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文