如何解决BadMethodCallException,调用未定义的方法Illuminate \ Database \ Query \ Builder :: filter()

时间:2019-06-04 17:57:23

标签: php laravel filter eloquent

我正在尝试从雄辩的查询中过滤结果,但出现下一个错误BadMethodCallException。据我说,我做的一切都正确。

我正在使用Laravel 5.4

错误详细信息:

BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::filter()
in Builder.php line 2445
at Builder->__call('filter', array(null))
in Builder.php line 1254
at Builder->__call('filter', array(null))
in web.php line 459
at Router->{closure}()
in Route.php line 189
at Route->runCallable()
in Route.php line 163
at Route->run()
in Router.php line 572

我有下一个代码:

public function index(SearchRequest $searchRequest, ConfigurationFilter $filters)
    {
        $filtered_configurations = Configuration::whereTrash(false)->with(['customs.properties', 'properties'])->filter($filters);

        $types = $this->getConfigurationTypes();
        $authors = $this->getAuthors();

        return view('configuration.assistant.index', [
            'configurations' => $filtered_configurations->paginate(10),
            'authors' => $authors,
            'types' => $types,
        ]);
    }

SearchRequest在哪里:

class SearchRequest extends FormRequest {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::user()->author != null;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

ConfigurationFilter在哪里:

class ConfigurationFilter extends ModelFilter
{
    public function name($value)
    {
        return $this->builder->where('name', 'like', "%{$value}%");
    }

    public function author($id)
    {
        return $this->builder->whereIn('user_id', explode(',', $id));
    }

    public function type($type)
    {
        return $this->builder->whereIn('category_id', explode(',', $type));
    }

    public function status($status)
    {
        return $this->builder->whereEnabled($status == 'enabled');
    }
}

ModelFilter在哪里:

class ModelFilter extends QueryFilter implements Filter
{
    public function id($id)
    {
        return $this->builder->whereIn('id', explode(',', $id));
    }

    public function from($date)
    {
        return $this->builder->where('created_at', '>=', $date);
    }

    public function to($date)
    {
        return $this->builder->where('created_at', '<=', $date);
    }

    public function enabled($status)
    {
        return $this->builder->whereEnabled($status === 'true');
    }

    public function trash($status)
    {
        return $this->builder->whereTrash($status === 'true');
    }

    public function take($limit = 100)
    {
        return $this->builder->take($limit);
    }
}

Filter在哪里:

interface Filter {

    public function id($id);

    public function from($date);

    public function to($date);

    public function enabled($status);

    public function trash($status);

    public function take($limit = 100);
}

我会丢失什么?

预先感谢

1 个答案:

答案 0 :(得分:1)

我已经解决了它,我在模型中添加了下一个函数:

public function scopeFilter($query, QueryFilter $filters)
{
    return $filters->apply($query);
}