In this episode, we're looking at how to pass extra data—like the request object—to each filter in a pipeline, instead of reaching for the request helper in every filter class. At first, it might seem like you could just pass multiple arguments into Laravel's pipeline, but out of the box, it only takes a single passable object.
To solve this, we try two different approaches. First, we create a custom object (like a DiscussionFilterObject
) that holds both the query builder and the request, letting us pass them together through the pipeline. This works, but feels a bit heavy-handed for simple use cases.
So, we switch gears and take inspiration from how middleware work in Laravel: by passing additional arguments (thanks to how parameters get passed into the handle
method via the route/middleware definition). By tweaking our filter setup to mirror this, we can push extra values (like an 'unsolved' flag from the request) directly into our filter classes as arguments.
In short, you'll see both a more formal (but heavier) method using a transfer object, and a lean, flexible method using pipeline arguments—allowing us to make our filters way easier to test. We wrap up by prepping for the next episode, where we'll write some tests for our new filter approach!