PHPackages                             laravelwakeup/filter-sort - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. laravelwakeup/filter-sort

ActiveLibrary[Search &amp; Filtering](/categories/search)

laravelwakeup/filter-sort
=========================

Filter Sort is a powerful Laravel package that supports searching and filtering saving your development time.

1.0.2(9mo ago)21.8kMITPHPPHP ^8.0

Since Mar 12Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/nguyenthanhthuc2000/laravel-filter-sort)[ Packagist](https://packagist.org/packages/laravelwakeup/filter-sort)[ RSS](/packages/laravelwakeup-filter-sort/feed)WikiDiscussions v1.0.x Synced today

READMEChangelogDependencies (1)Versions (6)Used By (0)

Filter Sort Scope for Laravel
=============================

[](#filter-sort-scope-for-laravel)

[![Release](https://camo.githubusercontent.com/7a3c6fbe4fae41bc6ed0d202af4aa7b4c570bccf8eef4bd6d46169bfa1b64f85/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c61726176656c77616b6575702f66696c7465722d736f72742e737667)](https://packagist.org/packages/laravelwakeup/filter-sort)[![Downloads](https://camo.githubusercontent.com/66a2df4878072d2c90cbd5d8bbb7125ba4cb6e4e6281af23e0b9f27f3c8d2b68/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c61726176656c77616b6575702f66696c7465722d736f72742e737667)](https://packagist.org/packages/laravelwakeup/filter-sort)[![License](https://camo.githubusercontent.com/7a9b84116c4519e6d48000f3d90d8e483120b98b62c51a09e1be5d7c0d4ffd0f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c61726176656c77616b6575702f66696c7465722d736f72742e737667)](https://github.com/nguyenthanhthuc2000/laravel-filter-sort?tab=MIT-1-ov-file)

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Requirements](#requirements)
- [Installation &amp; Configuration](#i-installation--configuration)
- [Basic Usage](#ii-basic-usage)
- [Available Operators](#iii-available-operators)
- [Examples](#iv-examples)
- [JavaScript Usage](#v-javascript-usage)
- [License](#vi-license)

Introduction
------------

[](#introduction)

This package provides `FilterTrait` and `SortTrait` to help you filter and sort data dynamically with various operators in Laravel Eloquent.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0
- Laravel &gt;= 8.0

I. Installation &amp; Configuration
-----------------------------------

[](#i-installation--configuration)

#### 1. Install

[](#1-install)

```
composer require laravelwakeup/filter-sort
```

#### 2. Publish Configuration

[](#2-publish-configuration)

```
php artisan vendor:publish --tag=laravel-filter-sort-config
```

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:

```
return [
    // Example: status_op=eq, status_sort=desc
    'prefix' => '_op',
    'sort_field_suffix' => '_sort'
];
```

II. Basic Usage
---------------

[](#ii-basic-usage)

### 1. Add Traits to Your Model

[](#1-add-traits-to-your-model)

```
use LaravelWakeUp\FilterSort\Traits\FilterTrait;
use LaravelWakeUp\FilterSort\Traits\SortTrait;

class Post extends Model
{
    use FilterTrait, SortTrait;

    // Optional: Restrict which fields can be filtered
    protected array $allowedFilters = ['title', 'created_at', 'status', 'deleted_at'];

    // Optional: Restrict which fields can be sorted
    protected array $allowedSorts = ['id', 'created_at'];
}
```

> **Note**: By default, if you don't define or set empty arrays for `$allowedFilters` and `$allowedSorts`, the package will allow filtering and sorting on all table fields.

### 2. Use in Controller

[](#2-use-in-controller)

```
$posts = Post::query()
    ->filter(request())
    ->sort(request())
    ->get();
```

III. Available Operators
------------------------

[](#iii-available-operators)

OperatorQuery StringDescription`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 &gt; '2023-01-01'`gte``created_at=2023-01-01&created_at_op=gte`Filter where created\_at &gt;= '2023-01-01'`lt``created_at=2023-01-01&created_at_op=lt`Filter where created\_at &lt; '2023-01-01'`lte``created_at=2023-01-01&created_at_op=lte`Filter where created\_at &lt;= '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 NULLIV. Examples
------------

[](#iv-examples)

### 1. Sorting

[](#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.

```
# Sort by single field
/posts?id_sort=desc

# Sort by multiple fields (applies in order of appearance)
/posts?created_at_sort=desc&id_sort=asc

# Combine with filters
/posts?title=Laravel&status=published&status_op=eq&created_at_sort=desc&id_sort=asc
```

#### 1.1 Sorting Parameters

[](#11-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 direction
    - `asc` for ascending order (default if invalid value provided)
    - `desc` for descending order

#### 1.2 Multiple Sort Example

[](#12-multiple-sort-example)

```
// Sort by created_at DESC, then by id ASC
/posts?created_at_sort=desc&id_sort=asc

// Sort by status DESC, created_at DESC, and id ASC
/posts?status_sort=desc&created_at_sort=desc&id_sort=asc
```

#### 1.3 Restrict Sortable Fields

[](#13-restrict-sortable-fields)

You can customize sorting behavior in your model:

```
protected array $allowedSorts = ['id', 'created_at', 'title', 'status'];
```

#### 1.4 Customize Sort Field Suffix

[](#14-customize-sort-field-suffix)

You can change the default `_sort` suffix by publishing the config file and modifying the `sort_field_suffix` value:

```
// config/laravel-filter-sort.php
return [
    'sort_field_suffix' => '_sort'  // Change this to your preferred suffix
];
```

> **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-search)

#### 2.1 Basic search

[](#21-basic-search)

```
# Fuzzy search (LIKE)
/posts?title=Laravel

# Exact match (Equal)
/posts?status=published&status_op=eq

# NULL check
/posts?deleted_at=1&deleted_at_op=null

# NOT NULL check
/posts?deleted_at=1&deleted_at_op=notNull
```

#### 2.2 Range Search

[](#22-range-search)

```
# Greater than
/posts?created_at=2023-01-01&created_at_op=gt

# Between range
/posts?created_at=2023-01-01,2023-12-31&created_at_op=between
```

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.

```

    Search

```

**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

[](#23-list-search)

```
# Filter by list (IN)
/posts?status=draft,pending&status_op=in

# Exclude list (NOT IN)
/posts?status=draft,pending&status_op=notIn
```

#### 2.4 Multi-Column Search

[](#24-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

[](#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.

```
protected array $multiColumnSearch = [
    'search_field' => 'search_txt', // The request parameter to use for the search term
    'fields' => [
        'username' => 'like',
        'server' => 'eq',
        // Add more fields as needed
    ],
];
```

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.

```

    Search

```

This will search the `username` and `server` fields in the `accounts` table using the specified operators.

V. JavaScript Usage
-------------------

[](#v-javascript-usage)

### 1. Using with qs library

[](#1-using-with-qs-library)

```
// Installation
// npm install qs
// yarn add qs

// Import
import qs from 'qs';
// or
const qs = require('qs');

// Example filters object
const filters = {
    // Normal filter
    name: 'John',
    name_op: 'like',

    // Filter with IN operator
    status: ['active', 'pending'],
    status_op: 'in',

    // Filter with BETWEEN operator
    created_at: ['2023-01-01', '2023-12-31'],
    created_at_op: 'between',

    // Filter with NULL operator
    deleted_at: '1',
    deleted_at_op: 'null',

    // Multiple field sorting
    created_at_sort: 'desc',
    id_sort: 'asc'
};

// Convert object to query string
const queryString = qs.stringify(filters, {
    arrayFormat: 'comma',    // Convert arrays to comma-separated strings
    encode: false            // Don't encode special characters
});
// Result: name=John&name_op=like&status=active,pending&status_op=in&created_at=2023-01-01,2023-12-31&created_at_op=between&deleted_at=1&deleted_at_op=null&created_at_sort=desc&id_sort=asc

// API call with Axios
axios.get(`/api/posts?${queryString}`);

// API call with Fetch
fetch(`/api/posts?${queryString}`);

// API call with jQuery
$.get(`/api/posts?${queryString}`);

// Parse query string back to object
const url = window.location.search; // ?name=John&name_op=like...
const parsed = qs.parse(url, {
    ignoreQueryPrefix: true,
    comma: true  // Parse comma-separated strings back to arrays
});

console.log(parsed);
// {
//     name: 'John',
//     name_op: 'like',
//     status: ['active', 'pending'],
//     status_op: 'in',
//     created_at: ['2023-01-01', '2023-12-31'],
//     created_at_op: 'between',
//     deleted_at: '1',
//     deleted_at_op: 'null',
//     created_at_sort: 'desc',
//     id_sort: 'asc'
// }
```

### 2. Using URLSearchParams (Browser built-in)

[](#2-using-urlsearchparams-browser-built-in)

```
// Create a new URLSearchParams instance
const params = new URLSearchParams();

// Add normal filter
params.append('name', 'John');
params.append('name_op', 'like');

// Add filter with IN operator
params.append('status', 'active,pending');  // Use string directly instead of array.join()
params.append('status_op', 'in');

// Add filter with BETWEEN operator
params.append('created_at', '2023-01-01,2023-12-31');  // Use string directly
params.append('created_at_op', 'between');

// Add filter with NULL operator
params.append('deleted_at', '1');
params.append('deleted_at_op', 'null');

// Add sorting
params.append('created_at_sort', 'desc');
params.append('id_sort', 'asc');

// Convert to query string and decode it
const queryString = decodeURIComponent(params.toString());
// Result: name=John&name_op=like&status=active,pending&status_op=in&created_at=2023-01-01,2023-12-31&created_at_op=between&deleted_at=1&deleted_at_op=null&created_at_sort=desc&id_sort=asc

// API calls
// With Fetch
fetch(`/api/posts?${queryString}`);

// With Axios
axios.get(`/api/posts?${queryString}`);

// With jQuery
$.get(`/api/posts?${queryString}`);
```

VI. License
-----------

[](#vi-license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance58

Moderate activity, may be stable

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~67 days

Total

4

Last Release

276d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/89718537?v=4)[Nguyễn Thành Thức](/maintainers/nguyenthanhthuc2000)[@nguyenthanhthuc2000](https://github.com/nguyenthanhthuc2000)

---

Top Contributors

[![nguyenthanhthuc2000](https://avatars.githubusercontent.com/u/89718537?v=4)](https://github.com/nguyenthanhthuc2000 "nguyenthanhthuc2000 (24 commits)")[![wake-thk](https://avatars.githubusercontent.com/u/185042590?v=4)](https://github.com/wake-thk "wake-thk (2 commits)")

---

Tags

laravelFilter sort package

### Embed Badge

![Health badge](/badges/laravelwakeup-filter-sort/health.svg)

```
[![Health](https://phpackages.com/badges/laravelwakeup-filter-sort/health.svg)](https://phpackages.com/packages/laravelwakeup-filter-sort)
```

###  Alternatives

[unopim/unopim

UnoPim Laravel PIM

10.5k2.4k](/packages/unopim-unopim)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[ecotone/laravel

Ecotone for Laravel — CQRS, Event Sourcing, Sagas, Durable Workflows, and Outbox on top of Laravel Queue, via PHP attributes.

21318.6k3](/packages/ecotone-laravel)[codewithdennis/larament

Larament is a time-saving starter kit to quickly launch Laravel 13.x projects. It includes FilamentPHP 5.x pre-installed and configured, along with additional tools and features to streamline your development workflow.

3991.8k](/packages/codewithdennis-larament)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
