PHPackages                             hamidrrj/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. hamidrrj/laravel-datatable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hamidrrj/laravel-datatable
==========================

Laravel Datatable is a package for handling server-side work of any table-like data with ease!

v0.1.0(1y ago)4171[3 PRs](https://github.com/hamidrezarj/laravel-datatable/pulls)MITPHPPHP ^8.1|^8.0CI passing

Since Aug 15Pushed 4mo ago1 watchersCompare

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

READMEChangelog (1)Dependencies (11)Versions (5)Used By (0)

Laravel Datatable
=================

[](#laravel-datatable)

[![Latest Version on Packagist](https://camo.githubusercontent.com/49198c52c3121a7466615733ce407da0d71c77f44b82c6110ff65e1d706e5706/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68616d696472726a2f6c61726176656c2d646174617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hamidrrj/laravel-datatable)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3052145845a313304c13f8eec8fb24522727345a486916746a12505ea2e57a89/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f68616d696472657a61726a2f6c61726176656c2d646174617461626c652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/hamidrezarj/laravel-datatable/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/95a50c1e75c47c0d551267e4a90599c3dcda3c2b4aae1e08324ca38bd153e17b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f68616d696472657a61726a2f6c61726176656c2d646174617461626c652f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/hamidrezarj/laravel-datatable/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/8779b0312fd5f4f057cc2d09c1bab99d9d746b50260df7de14afb12339e187ac/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68616d696472726a2f6c61726176656c2d646174617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hamidrrj/laravel-datatable)

Laravel Datatable is a package designed to handle server-side logic for datatables in Laravel applications.

Key Features
------------

[](#key-features)

- Standalone server-side solution for table-like data handling
- Compatible with various frontend table libraries (e.g., [Material React Table](https://www.material-react-table.com/))
- Support for multiple search logics (contains, equals, greater than, etc.) across different data types (numeric, text, date)
- Fine-grained control over searchable, sortable, and visible fields
- Ability to search through model relationships
- Customizable search logic (coming soon!)

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

[](#requirements)

- `PHP ≥ 8.1`
- `Laravel ≥ 9.0`

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

[](#installation)

You can install the package via composer:

```
composer require hamidrrj/laravel-datatable
```

After installation, publish the package's service provider using one of the following methods:

#### Option 1: Automatic Installation (Recommended)

[](#option-1-automatic-installation-recommended)

Run the following Artisan command:

```
php artisan datatable:install
```

#### Option 2: Manual Installation

[](#option-2-manual-installation)

Publish the provider manually:

```
php artisan vendor:publish --tag="datatable-provider"
```

Then, add the following line to the providers array in `config/app.php`:

```
return [
    // ...
    'providers' => ServiceProvider::defaultProviders()->merge([
        // ...
        App\Providers\DatatableServiceProvider::class,
        // ...
    ])->toArray(),
    // ...
];
```

Usage
-----

[](#usage)

This section covers various use cases and features of Laravel Datatable. From basic querying to advanced filtering and relationship handling, you'll find examples to help you make the most of this package.

### Table of Contents

[](#table-of-contents)

- [Method Parameters](#method-parameters)
- [Filter Array Structure](#filter-array-structure)
- [Return Data Structure](#return-data-structure)
- [Basic Usage](#basic-usage)
- [Using Query Builder](#using-query-builder)
- [Advanced Filtering and Sorting](#advanced-filtering-and-sorting)
- [Using `between` Search Function](#using-between-search-function)
- [Filtering Model's Relationship](#filtering-models-relationship)

### Method Parameters

[](#method-parameters)

The `run` method of `DatatableFacade` accepts the following parameters:

1. `$mixed`: Model instance or query builder instance to perform queries on.
2. `$requestParameters`: Contains parameters like `filter`, `sorting`, `size`, and `start` of required data.
3. `$allowedFilters`: (Optional) Specifies columns users are allowed to filter on.
4. `$allowedSortings`: (Optional) Specifies columns users are allowed to sort on.
5. `$allowedSelects`: (Optional) Specifies which columns users can actually see.
6. `$allowedRelations`: (Optional) Specifies which model relations users are allowed to filter on.

### Filter Array Structure

[](#filter-array-structure)

Each filter in the `filters` array should have the following attributes:

- `id`: Name of the column to filter on. When filtering a relationship's attribute, use the format: `relationName.attribute`. (`relationName` must exist as a `HasOne` or `HasMany` relationship in the base Model, e.g., User model)
- `value`: Value of the filter
    - For most filter types: a single value
    - For `fn = 'between'`: an array of two values, e.g., `[min, max]`
- `fn`: Type of filter to apply. Available options include:
    - `contains`
    - `between`
    - `equals`
    - `notEquals`
    - `lessThan`
    - `lessThanOrEqual`
    - `greaterThan`
    - `greaterThanOrEqual`
- `datatype`: Type of column. Options include:
    - `text`
    - `numeric`
    - `date`

### Return Data Structure

[](#return-data-structure)

The `run` method returns an array with the following structure:

```
[
    "data" => [
        // Array of matching records
    ],
    "meta" => [
        "totalRowCount" => 10 // Total count of matching records
    ]
]
```

### Basic Usage

[](#basic-usage)

Here's a simple example of requesting a chunk of 10 users starting from the 11th record (i.e., page 2 of the datatable):

```
use \HamidRrj\LaravelDatatable\Facades\DatatableFacade;

$userModel = new User();

$requestParameters = [
    'start' => 10,
    'size' => 10,
    'filters' => [],
    'sorting' => []
];

$data = DatatableFacade::run(
    $userModel,
    $requestParameters
);
```

### Using Query Builder

[](#using-query-builder)

You can use a query builder instance instead of a model instance:

```
$query = User::query()->where('username', '!=', 'admin');

$data = DatatableFacade::run(
    $query,
    $requestParameters
);
```

### Advanced Filtering and Sorting

[](#advanced-filtering-and-sorting)

Here's an example of filtering users whose ages are greater than 15, sorted by creation date in descending order:

```
$query = User::query();

$requestParameters = [
    'start' => 10,
    'size' => 10,
    'filters' => [
        [
            'id' => 'age',
            'value' => 15,
            'fn' => 'greaterThan',
            'datatype' => 'numeric'
        ]
    ],
    'sorting' => [
        [
            'id' => 'created_at',
            'desc' => true,
        ]
    ]
];

$allowedFilters = ['age'];
$allowedSortings = ['created_at'];

$data = DatatableFacade::run(
    $query,
    $requestParameters,
    $allowedFilters,
    $allowedSortings
);
```

**Note**: Ensure that columns used for filtering and sorting are included in the `$allowedFilters` and `$allowedSortings` arrays to avoid `InvalidFilterException` and `InvalidSortingException`.

### Using `between` search function

[](#using-between-search-function)

Here's an example of filtering users whose creation dates are between two dates:

```
$query = User::query()

$requestParameters = [
        'start' => 0,
        'size' => 10,
        'filters' => [
            [
                'id' => 'created_at',
                'value' => ['2024-05-23 10:30:00', '2024-05-29 15:00:00'],
                'fn' => 'between',
                'datatype' => 'date'
            ]
        ],
        'sorting' => []
    ];

$allowedFilters = array('created_at');
$allowedSelects = array('username', 'age', 'created_at');

$data = (new Datatable())->run(
    $query,
    $requestParameters,
    $allowedFilters,
    allowedSelects: $allowedSelects
);
```

**Note**: Using `$allowedSelects` will only return specified columns in the query result:

```
[
    "data" => [
        [
            'username' => 'mwindler'
            'age' => 49
            'created_at' => '2024-05-23T12:00:00.000000Z'
        ],
        // more matching records
    ],
    "meta" => [
        "totalRowCount" => 10 // Total count of matching records
    ]
]
```

### Filtering Model's Relationship

[](#filtering-models-relationship)

In this example, we filter only users who have posts that contain 'my post' in their titles:

```
$query = User::query();

$requestParameters = [
    'start' => 0,
    'size' => 10,
    'filters' => [
        [
            'id' => 'posts.title',
            'value' => 'my post',
            'fn' => 'contains',
            'datatype' => 'text'
        ]
    ],
    'sorting' => []
];

$allowedFilters = array('posts.title');
$allowedRelations = array('posts');

$data = (new Datatable())->run(
    $query,
    $requestParameters,
    $allowedFilters,
    allowedRelations: $allowedRelations
);
```

**Note**:

- Use `posts.title` in `id` (the User model must have a `posts` relation defined in `Models/User` class)
- Using `$allowedRelations` loads each user's posts in the query result:

```
[
    "data" => [
        [
            'id' => 1,
            'username' => 'sth',
            'posts' => [  // posts included in result
                [
                    'title' => 'wow! my post got 1k impressions!'
                ],
                // ...
            ]
        ],
        // more matching records
    ],
    "meta" => [
        "totalRowCount" => 10 // Total count of matching records
    ]
]
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Hamidreza Ranjbarpour](https://github.com/hamidrezarj)

License
-------

[](#license)

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

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance57

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 80% 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

Unknown

Total

1

Last Release

641d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/10ddba4908dd21b2ce2742cb691e41707ad39d8a69557d300d8664a7ee1994aa?d=identicon)[hamidrezarj](/maintainers/hamidrezarj)

---

Top Contributors

[![hamidrezarj](https://avatars.githubusercontent.com/u/53371031?v=4)](https://github.com/hamidrezarj "hamidrezarj (40 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")

---

Tags

datatableslaravellaravel-datatablesmaterial-react-tablelaravellaravel-datatablehamidRrj

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[marcelweidum/filament-expiration-notice

Customize the livewire expiration notice

9169.0k4](/packages/marcelweidum-filament-expiration-notice)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

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