PHPackages                             dykhuizen/laravel-datatable - 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. dykhuizen/laravel-datatable

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

dykhuizen/laravel-datatable
===========================

A Laravel package that contains queries for constructing a datatable

v2.1.3(5y ago)41.3kMITPHPPHP ^7.3 | ^8.0CI failing

Since Apr 28Pushed 5y ago2 watchersCompare

[ Source](https://github.com/gofish543/laravel-datatable)[ Packagist](https://packagist.org/packages/dykhuizen/laravel-datatable)[ Docs](https://github.com/gofish543/laravel-datatable)[ RSS](/packages/dykhuizen-laravel-datatable/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (4)Versions (15)Used By (0)

Datatable Laravel 7+
====================

[](#datatable-laravel-7)

Package for converting a request into a datatable response with searching, sorting, filtering, pagination, and data minimization in Laravel 7+.

Setup
=====

[](#setup)

Composer
--------

[](#composer)

Pull this package in through Composer

```
composer require dykhuizen/laravel-datatable
```

Usage
=====

[](#usage)

Use **Datatable** trait inside your *Eloquent* model(s). This single trait gives you access the following traits:

- Sortable - Applies a list of order by queries for specified columns
- Searchable - Applies a search query for selected columns
- Filterable - Applies a list of filters to the query based on columns selected
- Paginateable - Either runs the `$eloquent->get()` or `$eloquent->paginate()` methods based on the request
- SimplePaginateable - Either runs the `$eloquent->get()` or `$eloquent->simplePaginate()` methods based on the request
- Selectable - Selects the response data to be returned

```
use Dykhuizen\Datatable;

use Illuminate\Database\Eloquent\Model;
use Dykhuizen\Datatable\Datatable;

class User extends Model
{
    use Datatable;
}
```

```
GET http://localhost:8080/api/users
sortColumns=name,id&
sortOrder=asc,desc&
searchColumns=name&
search=testingName&
page=1&
per_page=5&
filter=deleted_at&
filter_deleted_at=false&
selectableFields=id,name,role.name
```

Sortable
--------

[](#sortable)

The sortable columns and order default to the keys `sortColumns` and `sortOrder` respectively.
These keys can be overwritten on a per model instance by setting the `$sortableColumnsKey` and `$sortableOrderKey` variables.
The values are expected to be comma delimited values and work with `HasOne` and `BelongsTo` relations

Ex:

```
sortColumns = 'id,username,role.id'
sortOrder = 'asc,desc,desc'

```

If you wish to have a field ordered which does not exist within a model's fields, you can optionally define a `[property]Sortable` function
Below is an address example

```
public function addressSortable($query, $direction)
{
    return $query->join('profiles', 'users.id', '=', 'profiles.user_id')->orderBy('address', $direction)->select('users.*');
}
```

Searchable
----------

[](#searchable)

The searchable columns and search default to the keys `searchColumns` and `search` respectively.
These keys can be overwritten on a per model instance by setting the `$searchableColumnsKey` and `$searchableSearchKey` variables.
The values are expected to be comma delimited values and work with any type of relation

Ex:

```
searchColumns = 'username,profile.address'
search = 'phrase to search on'

```

If you wish to have a field searched which does not exist within a model's fields, you can optionally define a `[property]Searchable` function
Below is an address example

```
public function addressSearchable($query, $search)
{
    return $query->whereHas('profile', function($query) use ($search)
    {
        return $query->where('address', '=', $search);
    });
}
```

Filterable
----------

[](#filterable)

The filterable column defaults to the key `filter`
This key can be overwritten on a per model instance by setting the `$filterableFieldsKey` variable.
The values are expected to be comma delimited values and work with any type of relation Additionally, the filter values are expected to be passed as unique fields. For example, filter on status expends the keys `filter = status` and `filter_status = 'yes,no,maybe` to exist

Ex:

```
filter = 'deleted_at,profile.deleted_at'
filter_deleted_at = 'false'
filter_profile.deleted_at = 'yes'

```

If you wish to have a field filtered which does not exist within a model's fields or has a unique filter attribute, you can optionally define a `[property]Filterable` function
Below is an address example

```
public function addressFilterable($query, $filters)
{
    return $query->where(function($query) use($filters) {
        foreach($filters as $filter) {
            switch($filters) {
                case 'local':
                    $query->orWhere(...);
                    break;
                case 'foreign':
                    $query->orWhere(...);
                    break;
                default:
                    $query->orWhere(...);
                    break;
            }
        }

        return $query;
    });
}
```

Paginateable and SimplePaginateable
-----------------------------------

[](#paginateable-and-simplepaginateable)

The paginateable columns default to `page` and `per_page` just like Laravel's built in pagination
These keys can be overwritten on a per model instance by setting the `$paginateablePageKey` and `$paginateablePerPageKey` variables.
If these keys exist, results will be paginated in the response. If these keys do not exist, the $forcePagination function param will be checked. If true, results will be paginated according to Laravel defaults. If false, results will be retrieved via the `get()` method.

Ex:

```
page = 1
per_page = 100

```

Selectable
----------

[](#selectable)

The selectable column defaults to `selectableFields`
This key can be overwritten on a per model instance by setting the `$selectableFieldsKey` variable
If this field exists, the selectable function will set a static property with these fields to be masked down to upon the call to `toArray()`
The goal of this trait is to reduce the response size when returning data to a front end application
For security reasons, the relation depth is maxed at 3 and if a relation is not loaded into the model before `toArray()` is called selectable will `abort()` out as someone is attempting to load relations dynamically which could cause data leakage

Ex:

```
selectableFields = id,profile.name,posts.id,posts.name

```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~19 days

Recently: every ~12 days

Total

14

Last Release

1951d ago

Major Versions

v0.2.0 → v1.0.02020-05-17

v1.0.6 → v2.0.02020-11-23

PHP version history (3 changes)v0.1.0PHP ^7.4

v0.2.0PHP ^7.3

v2.1.0PHP ^7.3 | ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/a12a3bb233da10f7a89e2983aa5e09cb9ee1b4cf01740df61891cc10ea5c12e7?d=identicon)[gofish543](/maintainers/gofish543)

---

Top Contributors

[![gofish543](https://avatars.githubusercontent.com/u/10394554?v=4)](https://github.com/gofish543 "gofish543 (25 commits)")

---

Tags

laravelfilteringsortingsearchingselectingpaginating

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dykhuizen-laravel-datatable/health.svg)

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

###  Alternatives

[kyslik/column-sortable

Package for handling column sorting in Laravel 6.x

6485.6M21](/packages/kyslik-column-sortable)[baijunyao/laravel-scout-elasticsearch

Elasticsearch Driver for Laravel Scout

8023.7k1](/packages/baijunyao-laravel-scout-elasticsearch)[omaressaouaf/query-builder-criteria

Define reusable query criteria for filtering, sorting, search, field selection, and includes in Laravel Eloquent models

282.4k](/packages/omaressaouaf-query-builder-criteria)[mobileka/scope-applicator

Scope Applicator is a PHP trait that makes data filtering and sorting easy.

251.2k2](/packages/mobileka-scope-applicator)

PHPackages © 2026

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