A simple trait for Laravel Eloquent models that allows you to easily filter your queries.
composer require firevel/filterable
- Add the
Filterable
trait to your Eloquent model. - Define the
$filterable
property on your model to specify which attributes can be filtered and their types.
use Firevel\Filterable\Filterable;
class User extends Model
{
use Filterable;
protected $filterable = [
'user_id' => 'id',
'name' => 'string',
'created_at' => 'datetime',
// ... other attributes ...
];
}
integer
date
datetime
id
string
relationship
boolean
json
array
Use the filter()
scope on your Eloquent query to apply filters.
// Fetch users with user_id equal to 5
$users = User::filter(['user_id' => ['=' => 5]])->get();
// Fetch users created after a certain date
$users = User::filter(['created_at' => ['>' => '2023-01-01']])->get();
// ... other filter combinations ...