Laravel 5-覆盖从口才获得或选择 [英] Laravel 5 - Override Get or Select from Eloquent

查看:83
本文介绍了Laravel 5-覆盖从口才获得或选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以重写Eloquent中的-> get()方法,以便始终自定义数据库查询中所选字段的输出?

Is it possible to override the ->get() methods from Eloquent in order to always customize the output of the selected fields from the DB query?

例如,如果您执行普通的口才查询,例如:

For example, if you do a usual Eloquent query like:

User::where('email','like','%wherever.com')->get();

代替查询:

Select name, email, address, created_at from users where email like '%wherever.com'

是否可以重写该方法以始终返回类似以下内容的内容:

Is it possible to override the method to always return something like:

Select CONCAT('CL::',name), lower(email), address, created_at from users where email like '%wherever.com'

我要求-> get,因为我已经看到可以传递一个包含要选择的列的数组,但是我不想在所有查询中都定义它们.

I ask for ->get because I have seen that I can pass an array with the columns to be selected but I don't want to define them on all queries.

推荐答案

因此,在深入研究了有关查询的功能之后,我发现了以下解决方案:

So, after digging around the functions regarding the query I found this solution:

我创建了一个新类 CustomQueryBuilder ,该类扩展了 Illuminate \ Database \ Query \ Builder

I created a new class CustomQueryBuilder that extends the Illuminate\Database\Query\Builder

您可以在Eloquent中覆盖 get()/select()/where()方法.

There you can override the get() / select() / where() methods from Eloquent.

然后,在要更改查询方式的模型中,定义要更改的字段,例如:

Then, in the models that you want to change the way the query is made, define the fields to change like:

protected $encrypted = [
    'name',
    'email',

];

此后,我创建了一个新类 CustomModel ,该类扩展了 Illuminate \ Database \ Eloquent \ Model ,并在那里覆盖了 newBaseQueryBuilder :

After this, I created a new class CustomModel that extends the Illuminate\Database\Eloquent\Model and there override the newBaseQueryBuilder like this:

protected function newBaseQueryBuilder()
{
    $connection = $this->getConnection();

    return new CustomQueryBuilder(
        $connection, $connection->getQueryGrammar(), $connection->getPostProcessor(), $this
    );
}

CustomQueryBuilder 中,您可以根据需要自定义builder的所有方法.

Inside the CustomQueryBuilder you can customise all the methods from builder for your needs.

使用此设置,您可以在任何 Illuminate \ Database \ Eloquent \ Model 中对其进行更改,以扩展您的 CustomModel ,并继承指定列的特殊行为.

With this setup, you can in any Illuminate\Database\Eloquent\Model change it to extend your CustomModel and inherit this special behaviour for the designated columns.

请注意,通过扩展您的CustomModel的模型进行的所有查询都将获得此新方法,因此请进行所有必要的检查,以免弄乱口才的正常行为,如下所示:

Be aware that all queries made from Models extending your CustomModel will get this new methods, so do all the needed checks to don't mess up with Eloquent normal behaviour, something like this:

public function where($column, $operator = null, $value = null, $boolean = 'and')
{

    if ($this->model !== null && isset($this->model::$encrypted)) 
    {
        if (in_array($column, $this->model::$encrypted)) 
        {
            $column = DB::raw("CONCAT('CL::',$column)");
        }
    }

    parent::where($column, $operator, $value, $boolean);

}

PS:对于CONCAT示例,我知道这听起来很愚蠢,但是使用$ encrypted属性,您可以发现它不是用于串联字符串.

PS: I know this sound silly with the CONCAT example but with the property $encrypted you can figure out that it's not for concatenating string.

这篇关于Laravel 5-覆盖从口才获得或选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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