PHPackages                             ymigval/laravel-model-datatable-ssp - 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. [Database &amp; ORM](/categories/database)
4. /
5. ymigval/laravel-model-datatable-ssp

ActiveLibrary[Database &amp; ORM](/categories/database)

ymigval/laravel-model-datatable-ssp
===================================

Extension designed to seamlessly integrate Laravel models with server-side DataTables. It provides a convenient and efficient way to fetch, transform, and display data from your Laravel models in DataTables.

v2.0.2(2y ago)9342MITPHPPHP &gt;=7.0.0

Since Sep 9Pushed 2y ago2 watchersCompare

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

READMEChangelog (7)Dependencies (2)Versions (8)Used By (0)

Processing Laravel Models with DataTables
=========================================

[](#processing-laravel-models-with-datatables)

Extension designed to seamlessly integrate Laravel models with [server-side DataTables](https://datatables.net/examples/server_side/simple.html). It provides a convenient and efficient way to fetch, transform, and display data from your Laravel models in DataTables.

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

[](#installation)

To get started, install the package via Composer:

```
composer require ymigval/laravel-model-datatable-ssp
```

Usage with Eloquent Models
--------------------------

[](#usage-with-eloquent-models)

To use DataTables with an Eloquent model, you can create an instance or query your model and call the `datatable()` method with column mappings.

```
use App\Models\Customer;

return (new Customer())->datatable([
    'first_name', 'last_name', 'phone'
]);
```

Alternatively, you can call the static `datatable()` method:

```
use App\Models\Customer;

return Customer::datatable([
    'first_name', 'last_name', 'phone'
]);
```

- Replace `Customer` with your Eloquent model.
- `['first_name' => 'first_name', 'last_name' => 'last_name', 'phone' => 'phone']`: Define the mappings of your model's fields.

### Customizing Columns

[](#customizing-columns)

You can customize field values by providing closures in your column mappings.

```
use App\Models.Customer;

return Customer::where('type', 'male')->datatable([
    'first_name',
    'last_name',
    'active' => function ($field, $row) {
        return ($field) ? 'Yes' : 'No';
    }
]);
```

- $field: Contains the value of the field in that mapping.
- $row: Contains the values of the fields in the context of the current row.

### Adding Additional Columns

[](#adding-additional-columns)

You can add additional columns by using closures:

```
use App\Models\Customer;

return Customer::datatable([
    'first_name',
    'last_name',
    function () {
        return 'additional column #1';
    },
    function () {
        return 'additional column #2';
    }
]);
```

### Fields in Context

[](#fields-in-context)

Define model fields in context to access related data or perform custom formatting.

```
use App\Models\Customer;

return Customer::datatable([
    'first_name' => function ($field, $row) {
        return $field . ' ' . $row->last_name;
    }
], ['last_name']);
```

By default, fields added to the context cannot be searched and sorted. You can configure this behavior by adding options to the field:

```
use App\Models\Customer;

return Customer::datatable([
    'first_name' => function ($field, $row) {
        return $field . ' ' . $row->last_name;
    }
], ['last_name' => ['orderable' => true, 'searchable' => true]]);
```

Usage with Query Builder
------------------------

[](#usage-with-query-builder)

You can use DataTable with Query Builder by calling `dataTable()` on a query builder instance.

```
use Illuminate\Support\Facades\DB;

return DB::table('customers')
    ->datatable([
        'first_name',
        'last_name',
        'phone'
    ]);
```

Transforming Output Data
------------------------

[](#transforming-output-data)

You can transform the datatable return into various formats such as `response`, `array`, or `json` by specifying it as the third parameter.

By default, a response is returned.

```
use App\Models\Customer;

(new Customer())->datatable(
    ['first_name', 'last_name', 'phone'],
    [],
    'array'
);
```

```
use Illuminate\Support\Facades\DB;

DB::table('customers')->datatable(
    ['first_name', 'last_name', 'phone'],
    [],
    'json'
);
```

Advanced Usage
--------------

[](#advanced-usage)

### Using Callbacks for Column Mappings

[](#using-callbacks-for-column-mappings)

You can use a callback to define columns dynamically.

```
use App\Models\Customer;

return (new Customer())->datatable(
    function () {
        return ['first_name', 'last_name', 'phone'];
    }
);
```

### Union Queries

[](#union-queries)

Perform union queries with DataTable.

```
use App\Models\Customer;

return Customer::join('business', 'business.id_customer', '=', 'customers.id')
    ->datatable(
        function () {
            return ['customers.first_name', 'customers.last_name', 'business.name'];
        }
    );
```

You can also add aliases to the fields in the column mapping or fields in context:

```
use App\Models\Customer;

return Customer::join('business', 'business.id_customer', '=', 'customers.id')
    ->datatable(
        [
            'customers.first_name AS f_name',
            'customers.last_name AS l_name',
            'business.name AS aaa',
        ],
        ['customers.phone AS contact' => ['orderable' => false, 'searchable' => true]]
    );
```

Using Eloquent Relationships
----------------------------

[](#using-eloquent-relationships)

#### Note on Using Relations

[](#note-on-using-relations)

When using relations, there are some limitations:

- Avoid using related fields as column values without a closure.
- To utilize the value of a related field, it should be accessed through a formatting closure. Use the second parameter of the closure to access the value. Remember that the second parameter contains the values of the fields in the context of the current row.
- Related fields cannot be sorted or searched.

Please make sure to add the local key used in the relation to your column mappings or fields in context.

```
use App\Models\Customer;

return Customer::with('business')
    ->datatable(
        [
            'first_name',
            'last_name',
            function ($field, $row) {
                return $row->business->name;
            },
        ],
        ['id'] // 'id' is the localKey field specified in the relation with 'business'
    );
```

For more usage examples, refer to the test cases.

Installing DataTables in Your Application
-----------------------------------------

[](#installing-datatables-in-your-application)

In the official DataTables documentation: , you will find the steps to install the library in your application.

Check out the server-side processing examples: [https://datatables.net/examples/server\_side/simple.html](https://datatables.net/examples/server_side/simple.html)

Changelog
---------

[](#changelog)

Please refer to the [CHANGELOG](CHANGELOG.md) for more information about recent changes.

License
-------

[](#license)

The MIT License (MIT). For more information, please see the [License File](LICENSE).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 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 ~23 days

Recently: every ~32 days

Total

7

Last Release

836d ago

Major Versions

v1.0.3 → v2.0.02024-01-04

### Community

Maintainers

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

---

Top Contributors

[![ymigval](https://avatars.githubusercontent.com/u/68402880?v=4)](https://github.com/ymigval "ymigval (11 commits)")

---

Tags

apidatatableseloquentlaravelmodelquerysspapilaravelmodeleloquentquerydatatablesssp

###  Code Quality

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/ymigval-laravel-model-datatable-ssp/health.svg)

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

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[jedrzej/searchable

Searchable trait for Laravel's Eloquent models - filter your models using request parameters

127259.1k5](/packages/jedrzej-searchable)[jedrzej/pimpable

Laravel 4/5/6 package that allows to dynamically filter, sort and eager load relations for your models using request parameters

105179.0k1](/packages/jedrzej-pimpable)[elipzis/laravel-cacheable-model

Automatic query-based model cache for your Laravel app

15546.1k](/packages/elipzis-laravel-cacheable-model)[omalizadeh/laravel-query-filter

A laravel package for resource filtering via request query string

163.0k](/packages/omalizadeh-laravel-query-filter)

PHPackages © 2026

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