PHPackages                             fazzinipierluigi/datatables-eloquent - 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. fazzinipierluigi/datatables-eloquent

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

fazzinipierluigi/datatables-eloquent
====================================

A Laravel library that uses Eloquent to process filters and sorting for the ColumnControl plugin for datatables.net.

00PHP

Since Feb 1Pushed 3mo agoCompare

[ Source](https://github.com/fazzinipierluigi/DatatablesEloquent)[ Packagist](https://packagist.org/packages/fazzinipierluigi/datatables-eloquent)[ RSS](/packages/fazzinipierluigi-datatables-eloquent/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

DatatablesEloquent
==================

[](#datatableseloquent)

This library provides a helper to process filters and sorting for the ColumnControl plugin for datatables.net.

Usage
-----

[](#usage)

Inside the method in your controller:

```
if($request->ajax())
{
    $users = \App\Models\User::select('*');

    $data_source = new DatatablesEloquent();
    $data_source->apply($users, $request);

    return $data_source->getResponse();
}
else
{
    return view('test');
}
```

You can pass the frontend timezone to the constructor (the default is 'Euurope/Rome'), alternatively you can set it later with the setTimezone method

```
$my_data_source = new DatatablesEloquent(); // This has as its timezone "Europe/Rome"

// These two options are equivalent
$data_source = new DatatablesEloquent("America/Chicago");
$data_source->setTimezone("America/Chicago");
```

Logically, all necessary libraries should be included in the frontend.

```

        Test

            new DataTable('#example', {
                columnControl: [
                    {
                        target: 0,
                        content: [
                            'order',
                            [
                                'orderAsc',
                                'orderDesc',
                                'spacer',
                                'orderAddAsc',
                                'orderAddDesc',
                                'spacer',
                                'orderRemove'
                            ]
                        ]
                    },
                    {
                        target: 1,
                        content: ['search']
                    }
                ],
                ordering: {
                    handler: false,
                    indicators: false
                },

                serverSide: true,
                ajax: '{{ \Illuminate\Support\Facades\URL::current() }}',
                colReorder: true,
                fixedColumns: true,
                columns: [
                    {
                        data: 'id', // Field name for searching/filtering
                        name: 'id', // field name for sorting
                        title: '#' // Display name
                    },
                    {
                        data: 'name', // Field name for searching/filtering
                        name: 'name', // field name for sorting
                        title: 'Nome' // Display name
                    },
                    {
                        data: 'email', // Field name for searching/filtering
                        name: 'email', // field name for sorting
                        title: 'Email' // Display name
                    },
                    {
                        data: 'created_at', // Field name for searching/filtering
                        name: 'created_at', // field name for sorting
                        title: 'Data di creazione', // Display name
                        type: 'date' // Force the field type otherwise it is recognized as a string
                    }
                ]
            });

```

The apply method accepts a third optional parameter, namely "field\_map", this parameter is used to map the database fields in case fields are renamed in the generation of the response or if more than one database column corresponds to a datagrid column:

```
public function index(Request $request)
{
	if($request->ajax())
	{
		$users = \App\Model\User::select('users.*');

		$data_source = new DatatablesEloquent();
		$field_map = [ // I create the array that contains the mapping field name => column name
			'fiscal_reference' => ['users.tax_code', 'users.vat'],
			'creation_date' => 'created_at'
		];
		$data_source->apply($users, $request, $field_map);

		return $data_source->getResponse(function($data) {
			// Optionally you can pass a Clojure function
			// to the "getResponse" function to format the
			// data or perform operations. If it is not
			// provided the toArray() method will be used.
			return [
				'username' => $data->username;
				'fiscal_reference' => ($data->is_company())? $data->vat : $data->tax_code;
				'creation_date' => $data->created_at->format('d/m/Y');
				// ... and so on
			];
		});
	}
	else
	{
		return view('user.index')
	}
}
```

The "apply" method also accepts an optional fourth parameter; this is used to override the sorting behavior:

```
public function index(Request $request)
{
	if($request->ajax())
	{
		$contracts = \App\Model\Contracts::select('contracts.*')
										 ->join('customers', 'customers.id', '=', 'contracts.customer_id');

		$data_source = new DatatablesEloquent();
		$field_map = [ // I create the array that contains the mapping field name => column name
			'customer' => 'customers.id'
		];
		$field_sorting = [
			'customer' => 'customers.company_name'
		];
		$data_source->apply($contracts, $request, $field_map, $field_sorting);

		return $data_source->getResponse(function($data) {
			// ....
		});
	}
	else
	{
		return view('customer.index')
	}
}
```

In this case if we had passed only the "field\_map" array to apply the sorting would have been done based on the customer id, instead by overriding it we can force the sorting by company name while still maintaining the search by id.

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance54

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1df4d6b7ede1ad143f400743960438450ae0a1721520e0b077de4ca7478f2c44?d=identicon)[GG91](/maintainers/GG91)

---

Top Contributors

[![fazzinipierluigi](https://avatars.githubusercontent.com/u/32109679?v=4)](https://github.com/fazzinipierluigi "fazzinipierluigi (2 commits)")

### Embed Badge

![Health badge](/badges/fazzinipierluigi-datatables-eloquent/health.svg)

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

###  Alternatives

[jacobbennett/pjax

PJAX for Laravel 5

11543.4k1](/packages/jacobbennett-pjax)[magefan/module-blog-m22

Fixes for Blog on Magento 2.2.x

2168.0k](/packages/magefan-module-blog-m22)[sbine/simple-tenancy

Simple Laravel multi-tenancy in the same database

373.4k](/packages/sbine-simple-tenancy)

PHPackages © 2026

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