mehradsadeghi/laravel-filter-querystring

how to pass filters as parameter

Closed this issue · 3 comments

I want to do the query operations by directing them to another class, not inside the controller. How can I pass filters in Request as an array parameter to the filter function?

        $filters = array_except($request->all(), ["page", "pageSize"]);
        $result = $this->queryBus->send(new GetListStatusTypeQuery($request->input("page"), $request->input("pageSize"), $filters));
        return $result;

controller


    public function Handle(GetListStatusTypeQuery $command)
    {
        $statusTypeList = StatusType::filter($command->filters)->get();
        return $statusTypeList;
    }

query handler

Hi @ahmet-cetinkaya .
I believe I didn't understand your question correctly. As you know you can retrieve incoming parameters from request() helper function all over your application (e.g. request('some_key')), And if you need to pass them into a function it must be possible like the pseudo code you wrote earlier.

Hi, thank you response.
I mean, I want to break the tight dependence on request(). I do not use API elements in the application layer in my projects. And I want to implement this with your package as well.
When we don't give a parameter, it can try to get the queries from request. When we give a filters object as a parameter, it filters by filter object. it can be more flexible and independent.

Take a look at this line please. As you can see the package (and the model using this trait) is being dependent on http layer (request()). This is a bad design which I was struggling with when I was writing the package (but I decided to have a more concise api for usage so I decided to do it this way).
My point is that if you're using this package, You're depending your application layer to the http layer inevitably and maybe using this package won't be a good idea in terms of your software's design.

But apart from that, as you mentioned you can use specific parameters for filter function. Assuming the request is consist of these parameters: $params = ['a', 'b', 'c']; and the filter function receives parameters as rest, So you need to pass them this way:

->filter(...$params)->;

I noticed you're design is CQS (or maybe CQRS). I don't know how exactly your application layers are connected to each other and how you might pass these params to the query bus, But I hope this would be helpful.