In this episode, we dive into implementing an ordering filter for our forum discussions. The main goal here is to allow users to sort the discussion list by the created_at
column, either in ascending or descending order. We walk through creating a new filter—created_at_order_query_filter
—and add it to our pipeline, showing how to hook it up in the Forum Index controller.
Along the way, you'll see why this kind of custom logic is okay for simple needs, but for more complex filtering and ordering, it's often better to use an existing package. We also cover a little gotcha with Laravel's query builder—if you call latest()
and then try to change the order by created_at
again, it won't work unless you reset the order, which we do in the episode.
To round things off, we quickly look at paginating the discussion results, just to show how easily it slots into the existing setup. We display the pagination links (even if they look a bit ugly for now) and mention where to output them in your Blade template.
Next up, we'll make our filters even more flexible and testable by learning how to pass additional arguments to them!