小编GON*_*ONG的帖子

Laravel 5.1使用全局范围的变形关系

我在我的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)

laravel laravel-5.1

6
推荐指数
0
解决办法
363
查看次数

标签 统计

laravel ×1

laravel-5.1 ×1