1. Go to this page and download the library: Download firevel/filterable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
firevel / filterable example snippets
// In app/Models/User.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Firevel\Filterable\Filterable;
class User extends Model
{
use Filterable;
/**
* Specify which fields (or “virtual” keys) can be filtered,
* along with their data types.
*/
protected $filterable = [
'id' => 'id',
'first_name' => 'string',
'last_name' => 'string',
'email' => 'string',
'created_at' => 'datetime',
];
}
class User extends Model
{
use Filterable;
protected $validateColumns = true;
protected $filterable = [
'id' => 'id',
'email' => 'string',
'status' => 'string',
];
}
// 1) Simple equality (defaults to '=')
$users = User::filter([ 'id' => 5 ])->get();
// → SELECT * FROM users WHERE id = 5;
// 2) Explicit operators
$users = User::filter([ 'created_at' => ['>' => '2024-01-01'] ])->get();
// → SELECT * FROM users WHERE created_at > '2024-01-01';
// 3) LIKE operator for strings
$users = User::filter([ 'email' => ['like' => '%@example.com'] ])->get();
// → SELECT * FROM users WHERE email LIKE '%@example.com';
$filters = [
'first_name' => ['like' => 'John'],
'created_at' => ['>=' => '2025-01-01'],
'status' => ['=' => 'active'],
];
$users = User::filter($filters)->get();
// → SELECT * FROM users
// WHERE first_name LIKE '%John%'
// AND created_at >= '2025-01-01'
// AND status = 'active';
use Illuminate\Database\Eloquent\Model;
use Firevel\Filterable\Filterable;
class User extends Model
{
use Filterable;
protected $filterable = [
'first_name' => 'string',
'last_name' => 'string',
'email' => 'string',
'created_at' => 'datetime',
// “name” isn’t a real column; mark it as a custom scope
'name' => 'scope',
];
// Add a local scopeName() to combine first_name OR last_name
public function scopeName($query, $value)
{
$query->where(function ($q) use ($value) {
$q->where('first_name', 'like', "%{$value}%")
->orWhere('last_name', 'like', "%{$value}%");
});
}
}
// Get users whose JSON “meta->role” equals “admin”
$users = User::filter([ 'meta->role' => ['=' => 'admin'] ])->get();
// → SELECT * FROM users WHERE JSON_EXTRACT(meta, '$.role') = 'admin';
protected $filterable = [
'tags' => 'array', // assumes tags is a JSON array column
];
// Get users whose “tags” array contains “premium”
$users = User::filter([ 'tags' => ['in' => 'premium'] ])->get();
// → SELECT * FROM users WHERE JSON_CONTAINS(tags, '"premium"');
// In User.php
protected $filterable = [
'email' => 'string',
'orders' => 'relationship',
];
// In Order.php (no special setup
// 1) Just check that a user has at least one order:
$usersWithAnyOrder = User::filter([ 'orders' => ['>' => 0] ])->get();
// → SELECT * FROM users
// WHERE ( SELECT COUNT(*) FROM orders WHERE orders.user_id = users.id ) > 0;
// 2) Filter by a condition on the order itself:
$filters = [
'orders.status' => ['=' => 'shipped'],
'email' => ['like' => '%@example.com'],
];
$users = User::filter($filters)->get();
// → SELECT * FROM users
// WHERE EXISTS (
// SELECT 1 FROM orders
// WHERE orders.user_id = users.id
// AND status = 'shipped'
// )
// AND email LIKE '%@example.com';
> // Define a custom where clause for the related model
> $relatedWhere = function ($query) {
> $query->where('price', '>', 100);
> };
>
> User::useRelationshipQuery($relatedWhere)
> ->filter([ 'orders' => ['in' => [1,2,3]] ])
> ->get();
>