Laravel Eloquent Query Builder 默认的 Where 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24484720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:43:30  来源:igfitidea点击:

Laravel Eloquent Query Builder Default Where Condition

phplaravellaravel-4eloquent

提问by Bilal Gultekin

I have News model, when i query news, i want it brings news where status = 1 as default.

我有新闻模型,当我查询新闻时,我希望它带来新闻,默认状态 = 1。

News::all(); // select * from news where status = 1
News::where('anotherColumn',2)->get(); // select * from news where status = 1 and where category = 2

Is this possible? What i want is so similar to soft delete feature (it gets where deleted_at is not null and if all data is wanted withTrashed function can be used).

这可能吗?我想要的与软删除功能非常相似(它获取deleted_at 不为空的地方,如果需要所有数据,可以使用Trashed 函数)。

I looked docs but i couldn't find anything helpful. Also, i tried to handle it in construct at News model but it didn't worked either.

我查看了文档,但找不到任何有用的信息。此外,我尝试在 News 模型的构造中处理它,但它也没有奏效。

Thanks.

谢谢。

回答by Unnawut

I normally override newQuery()for this. newQuery()is the method that Eloquent use to construct a new query.

我通常newQuery()为此覆盖。newQuery()是 Eloquent 用来构造新查询的方法。

class News extends Eloquent {

    public function newQuery($excludeDeleted = true) {
        return parent::newQuery($excludeDeleted)
            ->where(status, '=', 1);
    }

}

Now your News::all()will only output your news with status = 1.

现在您News::all()将只输出 status = 1 的新闻。

回答by Kylie

I think the closes you'll get, without actually going in to change some core files...

我认为你会得到关闭,而无需实际更改一些核心文件......

is Query Scope...

是查询范围...

Scopes allow you to easily re-use query logic in your models. To define a scope, simply prefix a model method with scope:

范围允许您轻松地在模型中重用查询逻辑。要定义范围,只需在模型方法前面加上范围:

class News extends Eloquent {
   public function scopeStatus($query)
    {
     return $query->where('status', '=', 1);
    }
 }

Utilizing that scope

利用该范围

 $news  = News::status()->get();
 $news2  = News::status()->where('anotherColumn',2)->get();

Its not quite what you wanted...but its definitely a little shorter than typing

它不是你想要的......但它绝对比打字短

 News::where('status','=',1)->get();

over and over

一遍又一遍

回答by Mickael Amar

It's been already mentioned but here is a quick example using global scope which might be the best current solution since you wont have to override Eloquent methods and would result into the same behavior but with more control of your model.

已经提到过,但这里是一个使用全局范围的快速示例,这可能是当前最好的解决方案,因为您不必覆盖 Eloquent 方法,并且会导致相同的行为,但可以更好地控制您的模型。

Just add this to your model :

只需将此添加到您的模型中:

protected static function boot()
{
    parent::boot();

    static::addGlobalScope('exclude_deleted', function (Builder $builder) {
        $builder->whereNull('deleted_at');
    });
}

You can also create a child Scope class and reuse it for multiple Models.

您还可以创建一个子 Scope 类并将其重用于多个模型。

For more information, Laravel doc explained pretty much everything about it: https://laravel.com/docs/5.6/eloquent#global-scopes

有关更多信息,Laravel 文档解释了几乎所有关于它的内容:https://laravel.com/docs/5.6/eloquent#global-scopes