如何在eloquent类中扩展方法get()
,添加一些在调用时进行连接的代码。
每当调用方法all()
,查找或get()
时,请添加该连接:
static::join('clientes', 'clientes.id', '=', 'faturas.cliente_id');
由于
答案 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;
}
}