Php 每次从特定模型更改查询生成器-Laravel 5.3

Php 每次从特定模型更改查询生成器-Laravel 5.3,php,laravel,laravel-5.3,Php,Laravel,Laravel 5.3,每次获取新模型时,如何更改查询生成器 我发现重写下面的方法是可行的,但我觉得这样做不太好。有更好的方法吗 因此,我希望每次对特定模型执行->join() 我不想将受保护的$with=[]属性,因为我不希望在不必要时执行额外的查询 public function newQueryWithoutScopes() { $builder = $this->newEloquentBuilder( $this->newBaseQueryBuilder() );

每次获取新模型时,如何更改查询生成器

我发现重写下面的方法是可行的,但我觉得这样做不太好。有更好的方法吗

因此,我希望每次对特定模型执行
->join()

我不想将
受保护的$with=[]属性,因为我不希望在不必要时执行额外的查询

public function newQueryWithoutScopes()
{
    $builder = $this->newEloquentBuilder(
        $this->newBaseQueryBuilder()
    );

    // Once we have the query builders, we will set the model instances so the
    // builder can easily access any information it may need from the model
    // while it is constructing and executing various queries against it.
    return $builder->setModel($this)->join('join statement comes here')->with($this->with);
}

要在每次使用模型时影响查询,可以使用newQuery方法

public function newQuery($excludeDeleted = true){
    return parent::newQuery()->join('orders', 'users.id', '=', 'orders.user_id');
}

但这可能会打破雄辩,因为您的模型不再代表您的数据库模型;它现在是一个由两个独立模型组成的Frankenmodel。

在Model类引导方法中使用Eloqunt全局作用域