The Laravel Filterable Trait is a powerful package providing a reusable trait for easily implementing filter functionality in your Laravel Eloquent models. It enables filtering query results based on multiple parameters, making it simpler to create dynamic and flexible APIs or data filtering mechanisms in your applications.
- Easily implement filtering on Eloquent models.
- Filter data based on various query parameters.
- Support for multiple filter types, such as exact matches, partial matches, ranges, etc.
- Customizable and extensible for adding new filter types.
- Improves code readability and maintainability by separating filtering logic from the main model.
You can install the package via Composer by running the following command:
composer require mehdirajabi/filterable
Open the Eloquent model where you want to apply the filtering functionality and add the Filterable trait:
use Mehdirajabi\Filterable\Filterable;
class User extends Model
{
use Filterable;
}
In your controller:
class UserController extends Controller
{
public function index()
{
return User::filter()->actived()->get();
}
}
Two query strings, filterColumns and filterValues, are used, both being arrays. The default operator for all columns is the "like" operator.
For example:
http://test.com/api/user/users?filterColumns[0]=name&filterValues[0]=Alex
To filter on a relation, add the column name as nested. For example, if a user has a relation with a profile and the profile has a 'sex' column, use the following convention:
use Mehdirajabi\Filterable\Filterable;
class User extends Model
{
use Filterable;
public function profile()
{
return $this->hasOne(UserProfile::class);
}
}
http://test.com/api/user/users?filterColumns[0]=profile.sex&filterValues[0]=male
To change the operator of the 'status' column, define the filterMode property:
use Mehdirajabi\Filterable\Operators\FilterEqual;
class User extends Model
{
protected $filterMode = [
'status' => FilterEqual::class
];
}
use Mehdirajabi\Filterable\Operators\FilterBetween;
class UserController extends Controller
{
public function index()
{
return User::filter(['id' => FilterBetween::class])->paginate(100);
}
}
use Mehdirajabi\Filterable\Operators\FilterBetween;
class User extends Model
{
protected $filterMode = [
'created_at' => FilterBetween::class
];
}
http://test.com/users?filterColumns[0]=created_at&filterValues[0]=2023-06-23 00:00:00, 2023-06-24 23:59:59