PHPackages                             teamq/laravel-datatables - 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. [API Development](/categories/api)
4. /
5. teamq/laravel-datatables

ActiveLibrary[API Development](/categories/api)

teamq/laravel-datatables
========================

Custom filter and sorting set for 'spatie/laravel-query-builder' package

3.1.2(2mo ago)05.0k↓42.9%[1 PRs](https://github.com/teamq-ec/teamq-laravel-datatables/pulls)MITPHPPHP &gt;=8.2CI passing

Since May 28Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/teamq-ec/teamq-laravel-datatables)[ Packagist](https://packagist.org/packages/teamq/laravel-datatables)[ Docs](https://github.com/teamq-ec/teamq-laravel-datatables)[ RSS](/packages/teamq-laravel-datatables/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (18)Versions (16)Used By (0)

Laravel Datatable API
=====================

[](#laravel-datatable-api)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7ef634c212513e291cfb48e0163e940e9b83572e2e0f42c741e6c2afc14522c7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7465616d712f6c61726176656c2d646174617461626c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/teamq/laravel-datatables)[![GitHub Tests Action Status](https://camo.githubusercontent.com/0753a8ba0cf481e2043990d01c437fcd2fc46478541df9b3984b32800d3c224c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7465616d712d65632f7465616d712d6c61726176656c2d646174617461626c65732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/teamq-ec/teamq-laravel-datatables/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/18ffb43a9a89ed0922aacd8b0eba96df54af1c381c0be1d57299bae6b0e73d62/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7465616d712d65632f7465616d712d6c61726176656c2d646174617461626c65732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/teamq-ec/teamq-laravel-datatables/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/8eb172ac5b64bdbe4f2eb946ba2a55919353a39dd1b4d7cafc463c32cde08356/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7465616d712f6c61726176656c2d646174617461626c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/teamq/laravel-datatables)

This is a collection of classes for filters and sorts, extending from the [spatie/laravel-query-builder](https://github.com/spatie/laravel-query-builder) package, in addition to providing the possibility of applying these filters and sorting in related models using join through from the [kirschbaum-development/eloquent-power-joins](https://github.com/kirschbaum-development/eloquent-power-joins)package.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require teamq/laravel-datatables
```

You can publish the config file with:

```
php artisan vendor:publish --tag="laravel-datatable-config"
```

This is the contents of the published config file:

```
return [
    'parameters' => [
        'per_page' => 'per_page',
    ],

    'per_page' => [
        // Represents the value to be sent by the user, to obtain all records, if using the result method.
        'all' => 'all',
    ],
];
```

Usage
-----

[](#usage)

### Pagination

[](#pagination)

This package uses Laravel pagination by default; However, it allows you to specify through parameters of queries, the number of records to obtain, you can even obtain all the records.

```
GET /books?per_page=30
```

It is recommended to use the `result` method instead of `paginate` or `get`. Since `result` encapsulates the logic of both yes, it is requested to show all, below `result` will use `get` but if you want to see a number of records below will use `paginate` with the amount provided.

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Filters\TextFilter;

$query = QueryBuilder::for(Book::class);

$query->result();
```

### Filters

[](#filters)

FilterClassOperatorsText`TeamQ\Datatables\Filters\TextFilter`TextNumber`TeamQ\Datatables\Filters\NumberFilter`NumberDate`TeamQ\Datatables\Filters\DateFilter`NumberGlobal`TeamQ\Datatables\Filters\GlobalFilter`-HasRelationship`TeamQ\Datatables\Filters\HasRelationshipFilter`- Text comparison operatorsComparison operatorKeyEqual$eqNot Equal$notEqStart With$startWithNot Start With$notStartWithEnd With$endWithNot End With$notEndWithContains$containsNot Contains$notContainsIn$inNot In$notInFilled$filledNot Filled$notFilled Number comparison operatorsComparison operatorKeyEqual$eqNot Equal$notEqGreater Than$gtGreater Than Or Equal$gteLess Than$ltLess Than Or Equal$lteBetween$betweenNot Between$notBetweenIn$inNot In$notInFilled$filledNot Filled$notFilledYou can use advanced filters that have support for multiple comparison operators. The available comparison operators are located in `TeamQ\Datatables\Enums\Comparators`

To use these advanced filters, just call them as custom filters:

```
GET /books?filter[isbn][value]=54213&filter[isbn][operator]=$lt
```

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Filters\TextFilter;

QueryBuilder::for(Book::class)
        ->allowedFilters([
            AllowedFilter::custom('isbn', new TextFilter()),
        ]);
```

If you want to handle relationships using join, you must pass `false` as the first parameter to the filter and pass the type of `join` to use.

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Filters\TextFilter;
use TeamQ\Datatables\Enums\JoinType;

QueryBuilder::for(Book::class)
        ->allowedFilters([
            AllowedFilter::custom('isbn', new TextFilter(false, JoinType::Inner)),
        ]);
```

#### *Text Filter*

[](#text-filter)

The following example uses the comparison operator `$endWith` isbn 54213

```
GET /books?filter[isbn][value]=54213&filter[isbn][operator]=$endWith
```

```
select *
from `books`
where lower(`isbn`) like '%54213'
```

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Filters\TextFilter;

QueryBuilder::for(Book::class)
        ->allowedFilters([
            AllowedFilter::custom('isbn', new TextFilter()),
        ]);
```

#### *Number Filter*

[](#number-filter)

The following example uses the comparison operator `$in` 1, 5 or 9

For this example an array of values was used. Arraying values is supported by all types of operators (text and number).

```
GET /books?filter[id][value][0]=1&filter[id][value][1]=5&filter[id][value][2]=9&filter[id][operator]=$in
```

```
select *
from `books`
where id in (1, 5, 9)
```

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Filters\NumberFilter;

QueryBuilder::for(Book::class)
        ->allowedFilters([
            AllowedFilter::custom('id', new NumberFilter()),
        ]);
```

#### *Date Filter*

[](#date-filter)

The following example uses the comparison operator `$notBetween`, created at 2019-08-01 and 2019-08-10

For this example an array of values was used. Arraying values is supported by all types of operators (text and number).

```
GET /books?filter[created_at][value][0]=2019-08-01&filter[created_at][value][1]=2019-08-10&filter[created_at][operator]=$notBetween
```

```
select *
from `books`
where created_at not between '2019-08-01' and '2019-08-10'
```

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Filters\DateFilter;

QueryBuilder::for(Book::class)
        ->allowedFilters([
            AllowedFilter::custom('created_at', new DateFilter()),
        ]);
```

#### *Global Filter*

[](#global-filter)

The global filter implements general search functionality for the model and its relationships.

This is not a global search engine between entities!!! For that you can use the package [spatie/laravel-searchable](https://github.com/spatie/laravel-searchable)

To use this filter, you must pass the model fields to be filtered or their relationships.

```
GET /books?filter[search]='Luis'
```

```
select *
from `books`
where (
          exists (select *
                  from `authors`
                  where `books`.`author_id` = `authors`.`id`
                    and lower(`authors`.`name`) LIKE '%Luis%')
              or
          lower(`books`.`title`) LIKE '%Luis%'
          )
```

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Filters\GlobalFilter;

QueryBuilder::for(Book::class)
        ->allowedFilters([
            AllowedFilter::custom('search', new GlobalFilter([
                'author.name',
                'title',
            ])),
        ]);
```

#### *Has Relationship Filter*

[](#has-relationship-filter)

This filter accepts two possible values:

- `1` to filter by "Has"
- `0` to filter by "Does not have"

Use this filter when you want the user to be able to filter on records that have associated models/relationships, For example, you may be interested in obtaining all authors who have books.

```
GET /books?filter[has_books]=1
```

```
select *
from `authors`
where exists (select * from `books` where `authors`.`id` = `books`.`author_id`)
```

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Filters\HasRelationshipFilter;

QueryBuilder::for(Author::class)
        ->allowedFilters([
            AllowedFilter::custom('has_books', new HasRelationshipFilter(), 'books'),
        ]);
```

---

### Sorts

[](#sorts)

SortClassRelation`TeamQ\Datatables\Sorts\RelationSort`Case`TeamQ\Datatables\Sorts\CaseSort`#### *RelationSort*

[](#relationsort)

To sort by fields of related tables you must use `join`, there is no easy way to do it from eloquent, so you can use `RelationSort`, this class receives the type of `join` as a parameter.

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Sorts\RelationSort;

QueryBuilder::for(Book::class)
        ->allowedSorts([
            AllowedSort::custom('author.name', new RelationSort(JoinType::Inner)),
        ])
```

#### *CaseSort*

[](#casesort)

If you use enums or states, where each enum or state is represented by a number, you may want to sort by name of that enum or state and not by the number, then you can use `CaseSort`.

You must pass an array `[$key => $value]`, which will be used to generate the sort.

As a second parameter you can specify the type of `Join` that you want to use, if the ordering is by the field of a related model. By default, it is `Inner Join`.

```
use Spatie\QueryBuilder\AllowedFilter;
use TeamQ\Datatables\QueryBuilder;
use TeamQ\Datatables\Sorts\CaseSort;

QueryBuilder::for(Book::class)
        ->allowedSorts([
            AllowedSort::custom('state', new CaseSort([
                1 => 'Active',
                2 => 'Rejected',
                3 => 'Deleted',
                4 => 'Completed',
            ])),
        ]);
```

Testing
-------

[](#testing)

Can use docker-compose to run

```
docker-compose exec app composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Luis Arce](https://github.com/luilliarcec)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance85

Actively maintained with recent releases

Popularity23

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 73.7% 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 ~54 days

Recently: every ~84 days

Total

13

Last Release

75d ago

Major Versions

1.2.0 → 2.0.02024-10-17

v2.x-dev → 3.0.02025-04-02

PHP version history (3 changes)1.0.0PHP ^8.2|^8.3

2.1.0PHP ^8.2|^8.3|^8.4

2.1.1PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/5067da7022cae6bdfae3c2af9ffd2292e29af34cb2681d2269a51e489c65085d?d=identicon)[manager@teamq.biz](/maintainers/manager@teamq.biz)

---

Top Contributors

[![luilliarcec](https://avatars.githubusercontent.com/u/27895611?v=4)](https://github.com/luilliarcec "luilliarcec (56 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (14 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")[![gabyteamq](https://avatars.githubusercontent.com/u/88790744?v=4)](https://github.com/gabyteamq "gabyteamq (1 commits)")

---

Tags

apidatatablelaravelpackageteamqspatielaravellaravel-query-builderdatatablesfiltersteamqsortings

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/teamq-laravel-datatables/health.svg)

```
[![Health](https://phpackages.com/badges/teamq-laravel-datatables/health.svg)](https://phpackages.com/packages/teamq-laravel-datatables)
```

###  Alternatives

[spatie/laravel-query-builder

Easily build Eloquent queries from API requests

4.4k26.9M220](/packages/spatie-laravel-query-builder)[spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

1.9k15.1M99](/packages/spatie-laravel-fractal)[spatie/laravel-sql-commenter

Add comments to SQL queries made by Laravel

1931.4M1](/packages/spatie-laravel-sql-commenter)[yajra/laravel-datatables-fractal

Laravel DataTables Fractal Plugin.

966.9M29](/packages/yajra-laravel-datatables-fractal)[rupadana/filament-api-service

A simple api service for supporting filamentphp

204103.8k7](/packages/rupadana-filament-api-service)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)

PHPackages © 2026

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