Download the PHP package laravelwakeup/filter-sort without Composer
On this page you can find all versions of the php package laravelwakeup/filter-sort. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download laravelwakeup/filter-sort
More information about laravelwakeup/filter-sort
Files in laravelwakeup/filter-sort
Package filter-sort
Short Description Filter Sort is a powerful Laravel package that supports searching and filtering saving your development time.
License MIT
Informations about the package filter-sort
Filter Sort Scope for Laravel
Table of Contents
- Introduction
- Requirements
- Installation & Configuration
- Basic Usage
- Available Operators
- Examples
- JavaScript Usage
- License
Introduction
This package provides FilterTrait and SortTrait to help you filter and sort data dynamically with various operators in Laravel Eloquent.
Requirements
- PHP >= 8.0
- Laravel >= 8.0
I. Installation & Configuration
1. Install
2. Publish Configuration
After running the command above, the laravel-filter-sort.php config file will be created in your config/ directory. You can adjust the following settings:
II. Basic Usage
1. Add Traits to Your Model
Note: By default, if you don't define or set empty arrays for
$allowedFiltersand$allowedSorts, the package will allow filtering and sorting on all table fields.
2. Use in Controller
III. Available Operators
| Operator | Query String | Description |
|---|---|---|
like (default) |
title=Laravel |
Filter data with LIKE "%Laravel%" |
eq |
status=published&status_op=eq |
Filter where status = 'published' |
gt |
created_at=2023-01-01&created_at_op=gt |
Filter where created_at > '2023-01-01' |
gte |
created_at=2023-01-01&created_at_op=gte |
Filter where created_at >= '2023-01-01' |
lt |
created_at=2023-01-01&created_at_op=lt |
Filter where created_at < '2023-01-01' |
lte |
created_at=2023-01-01&created_at_op=lte |
Filter where created_at <= '2023-01-01' |
between |
created_at=2023-01-01,2023-12-31&created_at_op=between |
Filter data within range |
notIn |
status=draft,pending&status_op=notIn |
Exclude values in the list |
in |
status=draft,pending&status_op=in |
Filter values in the list |
null |
deleted_at=1&deleted_at_op=null |
Filter where field is NULL |
notNull |
deleted_at=1&deleted_at_op=notNull |
Filter where field is NOT NULL |
IV. Examples
1. Sorting
The package provides a simple and flexible way to sort your data. Sorting is applied only when sort parameters are present in the request.
1.1 Sorting Parameters
For any field you want to sort by (e.g., id, created_at, title), append _sort to the field name:
{field}_sort: Set the sort directionascfor ascending order (default if invalid value provided)descfor descending order
1.2 Multiple Sort Example
1.3 Restrict Sortable Fields
You can customize sorting behavior in your model:
1.4 Customize Sort Field Suffix
You can change the default _sort suffix by publishing the config file and modifying the sort_field_suffix value:
Note: Sorting is only applied when sort parameters are provided in the request. The order of sorting follows the order of parameters in the URL.
2. Search
2.1 Basic search
2.2 Range Search
Use two separate inputs for the start and end of the range. These inputs must have the suffix _start_range and _end_range, which are fixed and required for the range query to work correctly.
Note: The suffixes _start_range and _end_range are fixed and must be used exactly as shown to ensure the range queries are processed correctly.
2.3 List Search
2.4 Multi-Column Search
The multi-column search feature allows you to search across multiple columns using a single search term. This is useful for implementing a search bar that can search across different fields in your database.
Define Multi-Column Search Configuration in Your Model
In your Eloquent model, define a multiColumnSearch property to specify which fields should be included in the search and the operators to use.
Suppose you have a search input in your form with the name search_txt. When the form is submitted, the filter scope will automatically apply the search term to the specified fields using the defined operators.
This will search the username and server fields in the accounts table using the specified operators.
V. JavaScript Usage
1. Using with qs library
2. Using URLSearchParams (Browser built-in)
VI. License
The MIT License (MIT). Please see License File for more information.