Refine will be launching soon 🎉 Sign up for the early access list to stay up to date.
Refine for Laravel
A package by Hammerstone

Creating a Filter

Hammerstone Refine has not launched yet. Sign up for the early access list to stay up to date!

Creating your first filter is as simple as creating a new class that extends Hammerstone\Refine\Filter. When you do that, you'll notice that there are two abstract methods that need to be implemented: initialQuery and conditions.

This is what your class would look like before we implement those methods:

1use Hammerstone\Refine\Filter;
3class EmployeeFilter extends Filter
5 public function initialQuery()
6 {
7 // @TODO
8 }
10 public function conditions()
11 {
12 // @TODO
13 }

Initial Query

The initialQuery method is the starting point for every filter. This allows you, as the developer, to add restrictions into the base query that cannot be changed by the end user. In many cases you can simply use an empty model query:

1public function initialQuery()
3 return Employee::query();
Code highlighting powered by, a Hammerstone product.

This works well in many cases, but not all of them. For instance, if you want to scope the entire filter by company_id, you can do so in the initialQuery:

1public function initialQuery()
3 return Employee::where('company_id', Company::current()->id);

Or maybe you want to add in a scope:

1public function initialQuery()
3 return Employee::active();

You're welcome to do anything you want in this method, as long as you return an Eloquent\Builder or Database\Builder class.


Conditions are where you define what your users can filter on. If you wanted to let your users filter based on the employee's name, you could add a "name" condition.

1public function conditions()
3 return [
4 TextCondition::make('name', 'Name')
5 ];

The package comes with many conditions out of the box that you can use to build your perfect filter. To learn more about conditions, jump to the conditions overview page.