Download the PHP package coderscantina/laravel-filter without Composer
On this page you can find all versions of the php package coderscantina/laravel-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download coderscantina/laravel-filter
More information about coderscantina/laravel-filter
Files in coderscantina/laravel-filter
Package laravel-filter
Short Description A filter object for Laravel/Eloquent models based on laracasts approach.
License MIT
Informations about the package laravel-filter
Filter Package from Coders' Cantina
A secure and optimized filter object for Laravel/Eloquent models based on the laracasts approach.
Features
- Simple and fluent API for filtering Eloquent models
- Security-focused with protection against SQL injection
- Sortable trait for complex sorting with relation support
- Range filter support for dates and numeric values
- Performance optimizations for large datasets
- Filter whitelisting for controlled access
- Comprehensive test suite
Getting Started
- Install this package
- Define your filters
- Apply them to your models
Install
Require this package with composer:
Basic Usage
Define a filter:
In your model:
In your controller:
Advanced Operators
The package supports advanced filtering with operator-based syntax for more expressive queries. This feature allows you to easily implement complex filtering logic with minimal code.
Supported Operators
The following operators are supported:
Operator | SQL Equivalent | Description |
---|---|---|
null |
IS NULL |
Matches null values |
!null |
IS NOT NULL |
Matches non-null values |
eq |
= |
Equals (default if no operator specified) |
neq |
!= |
Not equals |
empty |
Custom | Matches empty strings or null values |
!empty |
Custom | Matches non-empty and non-null values |
like |
LIKE |
Contains (adds wildcards around value) |
!like |
NOT LIKE |
Does not contain (with wildcards) |
^like |
LIKE |
Starts with (adds wildcard after) |
like$ |
LIKE |
Ends with (adds wildcard before) |
lt |
< |
Less than |
gt |
> |
Greater than |
lte |
<= |
Less than or equal to |
gte |
>= |
Greater than or equal to |
in |
IN |
In a list of values (comma-separated) |
!in |
NOT IN |
Not in a list of values (comma-separated) |
Basic Usage
To use advanced operators, extend from the AdvancedFilter
class instead of ExtendedFilter
:
Request Examples
Advanced Use Cases
Using with Range Filters
The advanced filtering still supports the range filter syntax (...
) while adding operator capabilities:
Using with Date Filters
Similarly, date filters can use both traditional range and operator syntax:
Arrays of Values with Operators
You can combine multiple operator conditions for the same field:
Custom Operators
You can extend the supported operators by adding your own:
Implementation Details
If you're integrating the advanced filter functionality into an existing filter:
- Use the
AdvancedFilterable
trait in your filter class - Extend from
AdvancedFilter
or add the trait to your custom filter class - Update your filter methods to use
applyDynamicFilter()
,applyAdvancedRangeFilter()
, orapplyAdvancedDateFilter()
Security Considerations
The AdvancedFilter
class maintains all the security features of the base Filter
class:
- Column name validation to prevent SQL injection
- Input sanitization for filter values
- Support for filter whitelisting
- Proper handling of null and empty values
Always use filter whitelisting in production to control which filters can be applied from user input:
Security Features
Filter Whitelisting
To enhance security, always specify which filters are allowed:
Input Sanitization
The package automatically sanitizes input to prevent SQL injection attacks. However, you should still validate your input in controllers using Laravel's validation system.
Advanced Features
Sortable Trait
The Sortable
trait which is included in the ExtendedFilter
offers sorting abilities:
Sort using foreign key relations:
Limit the number of sort columns for performance:
Restrict sortable columns:
Range Filters
Apply range filters in various formats:
Date Range Filters
Filter by date ranges with automatic formatting:
Pagination Support
Apply limit and offset for pagination:
Performance Optimizations
The package includes several optimizations:
- Join caching for repeated relation sorting
- Maximum sort column limits
- Efficient array handling
- Targeted query building
Extending
Custom Filter Methods
Create custom filter methods in your filter class:
Override Core Methods
You can override core methods for custom behavior:
Testing
The package includes a comprehensive test suite:
Security Best Practices
- Always use filter whitelisting with
setWhitelistedFilters()
- Validate input in your controllers
- Limit sortable columns to prevent performance issues
- Use type-hinting in your filter methods
- Test your filters thoroughly
Change Log
Please see CHANGELOG for more information on what has changed recently.
All versions of laravel-filter with dependencies
illuminate/database Version ^8.0||^9.0||^10.0||^11.0||^12.0
illuminate/http Version ^8.0||^9.0||^10.0||^11.0||^12.0
illuminate/support Version ^8.0||^9.0||^10.0||^11.0||^12.0