如何在eloquent中扩展get()方法

时间:2013-09-03 14:56:13

标签: php laravel eloquent

如何在eloquent类中扩展方法get(),添加一些在调用时进行连接的代码。

每当调用方法all(),查找或get()时,请添加该连接:

static::join('clientes', 'clientes.id', '=', 'faturas.cliente_id');

由于

1 个答案:

答案 0 :(得分:1)

你试过eager loading吗?您还可以在模型中设置protected $with = array('clients');

您还可以通过扩展Model类来重载newQuery方法。

class BaseModel extends Model {

    public function newQuery($excludeDeleted = true)
    {
        $builder = parent::newQuery($excludeDeleted);

        $builder->join('clientes', 'clientes.id', '=', 'faturas.cliente_id');

        return $builder;
    }
}