我在我的Product模型和方法上有全局范围withChildren来获取范围内的数据.一切都很好,直到我尝试使用变形关系.
码
范围代码
public function apply(Builder $builder, Model $model)
{
return $builder->whereNull('parent_id');
}
/**
* Remove the scope from the given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function remove(Builder $builder, Model $model)
{
$query = $builder->getQuery();
foreach ((array) $query->wheres as $key => $where)
{
if($where['column'] === 'parent_id')
{
unset($query->wheres[$key]);
$query->wheres = array_values($query->wheres);
}
}
}
Run Code Online (Sandbox Code Playgroud)
withChildren 方法
public function scopeWithChildren()
{
return with(new static)->newQueryWithoutScope(new ParentScope); …Run Code Online (Sandbox Code Playgroud)