雄辩模型的默认范围? [英] Default scope for Eloquent-models?

查看:98
本文介绍了雄辩模型的默认范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个示例数据库表( users ):

Here is a sample database-table (users):

id - int(11) auto_increment
name - varchar(100)
banned - int(1)

禁止是一个布尔值,它是 0 false )。如果用户被禁止,该值为 1

The column banned is a boolean value which is 0 (false) by default. If an user has been banned, the value is 1.

我想排除任何默认情况下禁止所有查询中的用户使用。我可以创建一个查询范围,然后在任何地方使用。但是,我更希望默认情况下只需要检查。

I'd like to exclude any banned users from all queries by default. I could create a query scope and then use that everywhere. However, I'd much more prefer simply having that check on by default.

我还可以创建一个 newQuery 我自己的方法就像这样:

I could also create a newQuery-method of my own, like so:

// Inside User-model, which extends Eloquent
public function newQuery($excludeDeleted = true)
{
    $builder = parent::newQuery($exludeDeleted);
    $builder->where('banned', '=', '0');
    return $builder;
}

但是,这样我就无法关闭此行为。我可能想在我的私人管理面板中看到被禁止的用户,但是无法使用这个限制,因为这个限制将被应用于通过口音完成的任何查询。

However, this way I wouldn't be able to switch off this behaviour. I might want to see the banned users in my private admin panel, but would not be able to, as this restriction would be applied to any query done via Eloquent.

任何关于如何解决这个问题的想法?

Any idea on how to solve this problem?

推荐答案

我强烈建议使用资源库设计模式进行数据库查询,而不是做直接的雄辩查询在控制器和无处不在。

I strongly suggest on using Repository Design Pattern for DB queries instead of doing direct Eloquent queries in controllers and everywhere.

// Quick example, not tested
class UserRepositoy { // You should create an interface and maybe super class for handling common cases like caching, find(), all() etc

    protected $include_banned = false;
    protected $model;

    public function __construct() // you can use DI here
    {
            $this->model = new User;
    }

    public function setIncludeBanned($bool)
    {
        $this->include_banned = $bool;
    }

    protected function includeBanned($query)
    {
        if ($this->include_banned) {
            return $query->where('banned', 1);
        }
    }

    public function find($id)
    {
        $res = $this->model->where('id', $id);

        $res = $this->includeBanned($res);

        return $res->get();
    }

}

现在您可以设置存储库类在哪里你需要查询,你有统一的API到呼叫。在Laravel中,在这里和那里传播小型雄辩的查询真的很容易,但从长远来看,更新/更改和应对将非常烦人。尝试搜索 Laravel Design Pattern ,并提供大量信息和示例。已经使我的日子已经有了。

Now you can instiate the repository class where ever you need queries and you have unified API to the calls. In Laravel it's really easy to spread small Eloquent queries here and there but in the long run it will be really annoying to update/change and cope. Try googling for Laravel Design Pattern and there's lots of information and examples about it. Has made my day couple of times already.

如果需要,这种模式也可以更容易地用其他的东西来淹没整个口才。

This pattern also makes easier to ditch the whole Eloquent with something else if necessary.

这篇关于雄辩模型的默认范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆