PHP code example of devaction-labs / filterable-package
1. Go to this page and download the library: Download devaction-labs/filterable-package 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/ */
devaction-labs / filterable-package example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DevactionLabs\FilterablePackage\Traits\Filterable;
class Expense extends Model
{
use Filterable;
protected array $filterMap = [
'search' => 'description',
'date' => 'expense_date',
];
protected array $allowedSorts = ['expense_date', 'amount'];
}
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use DevactionLabs\FilterablePackage\Filter;
use App\Models\Expense;
class ExpenseController extends Controller
{
public function index()
{
$expenses = Expense::query()
->filtrable([
Filter::like('description', 'search'),
Filter::exact('expense_date', 'date'),
Filter::between('expense_date', 'date_range'),
Filter::json('attributes', 'user.name', 'LIKE', 'user_name'),
Filter::json('attributes', 'user.age', '>', 'user_age'),
Filter::relationship('user', 'name')->setValue('John')->with(),
Filter::relationship('user', 'name')->whereAny([
['name', '=', 'John'],
['email', '=', '[email protected]'],
])->with(),
])
->customPaginate(false, ['per_page' => 10, 'sort' => '-created_at']);
return response()->json($expenses);
}
}
// Create a date filter that will convert string dates to Carbon instances
$dateFilter = Filter::exact('created_at')->castDate();
// Apply to a query
$model->filtrable([$dateFilter]);
// Filter by date with time set to 23:59:59
$dateFilter = Filter::exact('created_at')->castDate()->endOfDay();
// Filter by date with time set to 00:00:00
$dateFilter = Filter::exact('created_at')->castDate()->startOfDay();
// The query will use the appropriate syntax for your database
$filter = Filter::json('attributes', 'user.age', '>', 'min_age');
// Manually specify database driver if needed
$filter = Filter::json('attributes', 'user.age', '>', 'min_age')->setDatabaseDriver('mysql');
// Match if ANY condition is true (OR logic)
$filter = Filter::relationship('user', 'name')
->whereAny([
['name', '=', 'John'],
['email', '=', '[email protected]'],
])
->with();
// Match if ALL conditions are true (AND logic)
$filter = Filter::relationship('user', 'name')
->whereAll([
['name', '=', 'John'],
['active', '=', true],
])
->with();
// Match if NONE of the conditions are true (NOT logic)
$filter = Filter::relationship('user', 'name')
->whereNone([
['banned', '=', true],
['deleted', '=', true],
])
->with();
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Product;
use DevactionLabs\FilterablePackage\Filter;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index(Request $request)
{
// Build a complex query using the Filterable trait
$products = Product::query()
// Define allowed sort fields and default sort
->allowedSorts(['name', 'price', 'created_at'], '-created_at')
// Define filter field mappings
->filterMap([
'search' => 'name',
'price_range' => 'price',
'date' => 'created_at',
'status_code' => 'status',
])
// Apply filters
->filtrable([
// Basic filters
Filter::like('name', 'search')
->setLikePattern('{{value}}%'), // Custom LIKE pattern (starts with)
// Numeric range filter
Filter::between('price', 'price_range'),
// Date filter with Carbon conversion
Filter::exact('created_at', 'date')
->castDate()
->endOfDay(), // Automatically set time to end of day
// JSON field filtering
Filter::json('attributes', 'specs.color', 'LIKE', 'color')
->setDatabaseDriver('mysql'),
Filter::json('attributes', 'specs.weight', '>', 'min_weight')
->setDatabaseDriver('mysql'),
// Relationship filter with eager loading
Filter::relationship('category', 'slug', '=', 'category')
->with(), // Eager load this relationship
// Complex relationship filter with conditional logic
Filter::relationship('tags', 'name')
->whereAny([
['name', '=', 'featured'],
['name', '=', 'sale'],
])
->with()
->setValue('has_special_tag'), // Custom value for this filter
// Multiple criteria for user permissions
Filter::relationship('user', 'id')
->whereAll([
['active', '=', true],
['role', '=', 'admin'],
])
->setValue(auth()->id()),
])
// Apply pagination with custom parameters
->customPaginate(false, [
'per_page' => $request->input('per_page', 15),
'sort' => $request->input('sort', '-created_at'),===========fffdfdfsdddddddddddsdsdsdsads
]);
return response()->json($products);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.