Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 像4.2一样,Laravel5.8在newQuery上应用全局范围_Php_Laravel - Fatal编程技术网

Php 像4.2一样,Laravel5.8在newQuery上应用全局范围

Php 像4.2一样,Laravel5.8在newQuery上应用全局范围,php,laravel,Php,Laravel,我正在将laravel 4.2项目升级到5.8 在4.2中,我使用全局范围特性自动将where条件添加到模型中。where条件将添加到“newQuery”上,并且是第一个where条件 现在在5.8中,我使用新的方式添加了全局范围,它没有在“newQuery”上添加范围中的where条件,而是通过将where条件附加到查询中,在“get”上应用全局范围条件。这与我的DB索引不匹配,我不能这样做,我需要在“newQuery”上应用我的全局范围 class TenantScope implement

我正在将laravel 4.2项目升级到5.8

在4.2中,我使用全局范围特性自动将where条件添加到模型中。where条件将添加到“newQuery”上,并且是第一个where条件

现在在5.8中,我使用新的方式添加了全局范围,它没有在“newQuery”上添加范围中的where条件,而是通过将where条件附加到查询中,在“get”上应用全局范围条件。这与我的DB索引不匹配,我不能这样做,我需要在“newQuery”上应用我的全局范围

class TenantScope implements Scope
{
  public function apply(Builder $builder, Model $model)
  {
    $builder->where($model->getQualifiedTenantColumn(), '=', \idweb\helpers\SessionHelper::getTenantId());
    $builder->whereNull($model->getQualifiedDateDeletedColumn());
  }
}

// when I do this:
$q = \myapp\MyModel::query();

// the following test should be true with my two where conditions applied from the scope
// with laravel 5.8, this fails, the scope has not been applied yet
$this->assertEquals(2, count($q->newQuery()->wheres));

$q->where('cat', 'dog');
$list = $q->get();

// this uses db query, which is wrong
select * from mymodel where cat=dog and tenant=1 and date_deleted is null;

// the query needs to be by having the scope applied on newQuery, not get:
select * from mymodel where tenant=1 and date_deleted is null and cat=dog;

如何像4.2一样在“newQuery”上应用我的作用域?

要为模型分配全局作用域,应覆盖给定模型的引导方法并使用addGlobalScope方法:


它没有文档记录,但您可以使用“扩展”而不是“应用”extend'在启动查询时激发,因此where子句位于第一位

在我的scope类中:

  /**
   * @param Builder $builder
   * @return void
   */
  public function extend(Builder $builder)

是的,这就是我正在做的。执行此操作时,作用域将应用于“get”。我需要将其应用于'newQuery',因此'where'子句是第一个条件,而不是最后一个条件。
  /**
   * @param Builder $builder
   * @return void
   */
  public function extend(Builder $builder)