PHPackages                             angusm73/laratables - 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. angusm73/laratables

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

angusm73/laratables
===================

Ajax support of DataTables for Laravel

v4(1y ago)044MITPHPPHP ^8.0

Since Nov 18Pushed 1y agoCompare

[ Source](https://github.com/angusm73/laratables)[ Packagist](https://packagist.org/packages/angusm73/laratables)[ RSS](/packages/angusm73-laratables/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (24)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/5f645ec02ab0573eafb1e9e4ac128dc701241f70d80e6ad33e3ea97645c9a546/68747470733a2f2f706f7365722e707567782e6f72672f6672657368626974737765622f6c6172617461626c65732f762f737461626c65)](https://packagist.org/packages/freshbitsweb/laratables)[![Total Downloads](https://camo.githubusercontent.com/dc4f1f450a306b77a54fb3793028d367eba2a12c9e04f5610dace86571f5912b/68747470733a2f2f706f7365722e707567782e6f72672f6672657368626974737765622f6c6172617461626c65732f646f776e6c6f616473)](https://packagist.org/packages/freshbitsweb/laratables)[![License](https://camo.githubusercontent.com/33b6da17869a9d1c0caef77251787d1e3469fbb3b015086e9fdb0e25557d3141/68747470733a2f2f706f7365722e707567782e6f72672f6672657368626974737765622f6c6172617461626c65732f6c6963656e7365)](https://packagist.org/packages/freshbitsweb/laratables)[![StyleCI](https://camo.githubusercontent.com/f31ac854ec72429dbc20a5a6ccc6e497811d66d52cf788419d4ca77515600087/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130383432313131392f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/108421119)[![Buy us a tree](https://camo.githubusercontent.com/dd118003b5a1a1654809a5c3915a5f9e213a16eeeb09781e1c5e0fe4c336f46b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4275792532306d6525323061253230747265652d2546302539462538432542332d6c69676874677265656e3f7374796c653d666c61742d737175617265)](https://plant.treeware.earth/freshbitsweb/laratables)

Package Status
==============

[](#package-status)

This package is not under active development. We no longer plan to add new features. The main reason being that our company projects do not use Datatables anymore.

If you wish to maintain a fork of this package, I would be more than happy to link.

Laratables
==========

[](#laratables)

A Laravel package to handle server side ajax of [Datatables](https://datatables.net).

Table of contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [How to use](#how-to-use)
    - [Client side](#client-side)
    - [Server side](#server-side)
- [Demo Repositories](#demo-repositories)
- [Installation](#installation)
- [Customization](#customization)
    - [Controlling the query](#controlling-the-query)
        - [Controlling the relationship query](#controlling-the-relationship-query)
        - [Joining related tables to the query](#joining-related-tables-to-the-query)
    - [Custom columns](#custom-columns)
    - [Relationship columns](#relationship-columns)
    - [Customizing column values](#customizing-column-values)
    - [Searching](#searching)
    - [Ordering (Sorting)](#ordering-sorting)
    - [Selecting additional columns](#selecting-additional-columns)
    - [Specifying additional searchable columns](#specifying-additional-searchable-columns)
    - [Date format for Carbon instances](#date-format-for-carbon-instances)
    - [Modify fetched records](#modify-fetched-records)
    - [Extra data- Datatables attributes](#extra-data--datatables-attributes)

Introduction
------------

[](#introduction)

This package helps with simple requirements of displaying data from eloquent models into datatables with ajax support. Plus, using simple relationships and customizing column values. Laratables does not work with Datatables Editor yet.

How to use
----------

[](#how-to-use)

The basic approach is that you can specify the Datatable configuration and columns on the client side just like you would without any major change and call a single method on the server side to handle ajax calls. The package will create necessary queries to fetch the data and make the search and ordering functionality work automatically. If needed, You can step-in and customize query/data/logic at various stages by adding methods in your Eloquent model or a custom class.

### Client side

[](#client-side)

```
$('#users-table').DataTable({
    serverSide: true,
    ajax: "{{ route('admin.users.datatables') }}",
    columns: [
        { name: 'id' },
        { name: 'name' },
        { name: 'email' },
        { name: 'role.name', orderable: false },
        { name: 'action', orderable: false, searchable: false }
    ],
    ...
});
```

⚠️ **IMP Note** ⚠️ - The client side code decides the columns that should be fetched. If you are showing the table on a public facing page, malicious users can modify the HTTP request to fetch data for other columns of respective table and related tables. It is highly recommend that you validate the requests before returning the data.

### Server side

[](#server-side)

```
use App\User;
use Freshbitsweb\Laratables\Laratables;
...
return Laratables::recordsOf(User::class);
```

There are multiple ways to customize query/data/logic. Check [Customization](#customization) section below for details.

**[⬆ back to top](#table-of-contents)**

Demo Repositories
-----------------

[](#demo-repositories)

1.
2.
3.
4.
5.
6.

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

[](#requirements)

PHPLaravelPackage8.2+11.xv4.0.08.0+10.xv3.0.08.0+9.xv2.5.07.3+8.xv2.4.07.2.5+7.xv2.3.0&lt;7.2.56.xv2.2.07.15.xv2.0.\*Installation
------------

[](#installation)

Install the package by running this command in your terminal/cmd:

```
composer require freshbitsweb/laratables
```

Optionally, you can import config file by running this command in your terminal/cmd:

```
php artisan vendor:publish --tag=laratables_config
```

**[⬆ back to top](#table-of-contents)**

Customization
-------------

[](#customization)

Following the steps of How to use section should get you up and running with a simple datatables example in a minute. However, many datatables require customization ability. First use case is about applying additional where conditions to the query or load additional relationships.

To achieve that, you can simply pass a closure/callable as a second parameter to `recordsOf()` method. It should accept the underlying query as a parameter and return it after applying conditionals:

```
use App\User;
use Freshbitsweb\Laratables\Laratables;
...
return Laratables::recordsOf(User::class, function($query)
{
    return $query->where('active', true);
});
```

There are many more options to customize query/data/logic. You can add any of the following methods (they start with `laratables` word) in your **model or a custom class** to keep your model neat and clean. Specify the name of the custom class as the second argument to the `recordsOf()` method if you wish to use one:

```
use App\User;
use Freshbitsweb\Laratables\Laratables;
use App\Laratables\User as UserLaratables;
...
return Laratables::recordsOf(User::class, UserLaratables::class);
```

**[⬆ back to top](#table-of-contents)**

### Controlling the query

[](#controlling-the-query)

If you wish to apply conditions everytime a model is used to display a Laratable, add `laratablesQueryConditions()` static method to the model/custom class.

```
/**
 * Fetch only active users in the datatables.
 *
 * @param \Illuminate\Database\Eloquent\Builder
 * @return \Illuminate\Database\Eloquent\Builder
 */
public static function laratablesQueryConditions($query)
{
    return $query->where('active', true);
}
```

This method accepts and returns a `$query` object.

#### Controlling the relationship query

[](#controlling-the-relationship-query)

You can also control the relationship query by defining a closure which can be used while eager loading the relationship records. The static method name format is `laratables[RelationName]RelationQuery`.

```
/**
 * Eager load media items of the role for displaying in the datatables.
 *
 * @return callable
 */
public static function laratablesRoleRelationQuery()
{
    return function ($query) {
        $query->with('media');
    };
}
```

#### Joining related tables to the query

[](#joining-related-tables-to-the-query)

The `laratablesQueryConditions()` method can also be used to add joins on the base table. This is particularly useful if you need to define custom searching and sorting based on related models, for example:

```
/**
 * Join roles to base users table.
 * Assumes roles -> users is a one-to-many relationship
 *
 * @param \Illuminate\Database\Eloquent\Builder
 * @return \Illuminate\Database\Eloquent\Builder
 */
public static function laratablesQueryConditions($query)
{
    return $query->join('roles', 'roles.id', 'users.role_id');
}
```

***Note*** - If searching/filtering by columns from the joined table, you will need to also ensure they are included in the selected columns. A couple of options for how to do this include:

- Chaining a `->select()` method above - e.g. `$query->join(...)->select(['roles.name as role_name'])`, or
- Using the `laratablesAdditionalColumns()` method as described in [Selecting additional columns](#selecting-additional-columns) below.

**[⬆ back to top](#table-of-contents)**

### Custom columns

[](#custom-columns)

Generally, we need one or more columns that are not present in the database table. The most common example is 'Action' column to provide options to edit or delete the record. You can add a static method `laratablesCustom[ColumnName]()` to your model/custom class to specify the custom column value. As per our example, it could be:

```
/**
 * Returns the action column html for datatables.
 *
 * @param \App\User
 * @return string
 */
public static function laratablesCustomAction($user)
{
    return view('admin.users.includes.index_action', compact('user'))->render();
}
```

As you may have observed, you receive an eloquent object of the record as a parameter to use the record details in your method.

**[⬆ back to top](#table-of-contents)**

### Relationship columns

[](#relationship-columns)

We also need to display data from related models, right? And it's super easy here. No need to do anything on server side for simple relationships. Just specify the name of the relation and the name of the column on the client side inside columns array.

```
columns: [
    ...
    { name: 'role.name', orderable: false },
    ...
],
```

Ordering records by a relationship column is not supported in Laravel as main table records are fetched first and another query is fired to fetch related table records. Therefore, you should always keep your relationship table columns to be `orderable: false`.

**Note** - The package does not support [nested relationships](https://github.com/freshbitsweb/laratables/issues/6) yet. You can utilize the custom column feature to get the nested relationship data but make sure that you [eager load](https://github.com/freshbitsweb/laratables#controlling-the-query) the relationship records.

**[⬆ back to top](#table-of-contents)**

### Customizing column values

[](#customizing-column-values)

Sometimes, you may need to customize the value of a table column before displaying it in the datatables. Just add a static method `laratables[ColumnName]()` in your eloquent model/custom class to play with that:

```
/**
 * Returns truncated name for the datatables.
 *
 * @param \App\User
 * @return string
 */
public static function laratablesName($user)
{
    return Str::limit($user->name, 15);
}
```

Relationship columns can also be customized by adding a static method in this format `laratables[RelationName][ColumnName]()`.

These static methods are called on the eloquent model/custom class with the eloquent object of the record as a parameter.

**Note** - These methods were regular methods in v1.\*.\*

**[⬆ back to top](#table-of-contents)**

### Searching

[](#searching)

Datatables provides searching functionality to filter out results based on any of the displayed columns values. While this package automatically applies *orWhere* conditions with like operator, you can put your own conditions for any column. We provide static method `laratablesSearch[ColumnName]()` for the same.

```
/**
 * Adds the condition for searching the name of the user in the query.
 *
 * @param \Illuminate\Database\Eloquent\Builder
 * @param string search term
 * @return \Illuminate\Database\Eloquent\Builder
 */
public static function laratablesSearchName($query, $searchValue)
{
    return $query->orWhere('first_name', 'like', '%'. $searchValue. '%')
        ->orWhere('surname', 'like', '%'. $searchValue. '%')
    ;
}
```

If any of the columns is a relationship column, the package is smart enough to apply `orWhereHas` query with necessary closure to filter out the records accordingly. And you can override that behaviour by adding `laratablesSearch[RelationName][ColumnName]()` static method to your eloquent model/custom class.

**Note** - You can add `searchable: false` to any of the columns in Datatables configuration (client side) to prevent searching operation for that column. Or you can also add the name of the column to the `non_searchable_columns` array in the config file.

**[⬆ back to top](#table-of-contents)**

### Ordering (Sorting)

[](#ordering-sorting)

Ordering for regular table columns works by default. Multi-column sorting is also supported. For relationship columns or custom columns, you should either add `orderable: false` to Datatables column configuration or add a static method `laratablesOrder[ColumnName]()` and return the name of main table column that should be used for ordering the records instead. For example, if your table contains *first\_name* and Datatables has just *name*, you can add:

```
/**
 * first_name column should be used for sorting when name column is selected in Datatables.
 *
 * @return string
 */
public static function laratablesOrderName()
{
    return 'first_name';
}
```

You can also order rows by a raw statement by adding a static method `laratablesOrderRaw[ColumnName]()` and return the raw statement that we would put in *orderByRaw()* of the query. The function receives the `$direction` variable which will be either *asc* or *desc*.

```
/**
 * first_name and last_name columns should be used for sorting when name column is selected in Datatables.
 *
 * @param string Direction
 * @return string
 */
public static function laratablesOrderRawName($direction)
{
    return 'first_name '.$direction.', last_name '.$direction;
}
```

**[⬆ back to top](#table-of-contents)**

### Selecting additional columns

[](#selecting-additional-columns)

We have coded the package in a way where the query selects only required columns from the database table. If you need values of additional column when you are customizing column values, you may specify them in an array inside `laratablesAdditionalColumns()` static method. For example, if you have *first\_name* and *surname* in the table and you're just using a custom column *name* instead, you can add:

```
/**
 * Additional columns to be loaded for datatables.
 *
 * @return array
 */
public static function laratablesAdditionalColumns()
{
    return ['first_name', 'surname'];
}
```

**[⬆ back to top](#table-of-contents)**

### Specifying additional searchable columns

[](#specifying-additional-searchable-columns)

If you need to search for values in columns of the table which aren't displayed in the Datatables, you may specify them in an array inside `laratablesSearchableColumns()` static method. For example, if you have *first\_name* and *surname* in the table and you want users to be able to search by those columns even if they are not displayed in the Datatables, you can add:

```
/**
 * Additional searchable columns to be used for datatables.
 *
 * @return array
 */
public static function laratablesSearchableColumns()
{
    return ['first_name', 'surname'];
}
```

**[⬆ back to top](#table-of-contents)**

### Date format for Carbon instances

[](#date-format-for-carbon-instances)

By default, Laravel treats *created\_at* and *updated\_at* as Carbon instances and you can also treat any other column of your table as a Carbon instance as well. This package has a config option `date_format` to specify the format in which the dates should be returned for Datatables. Default format is 'Y-m-d H:i:s'.

### Modify fetched records

[](#modify-fetched-records)

Sometimes, we need to work with the records after they are already fetched. You can add `laratablesModifyCollection()` static method to your model/custom class to play with the collection and return the updated one. Note that if you add/remove any items from the collection, the Datatables count will have a mismatch.

```
/**
 * Set user full name on the collection.
 *
 * @param \Illuminate\Support\Collection
 * @return \Illuminate\Support\Collection
 */
public static function laratablesModifyCollection($users)
{
    return $users->map(function ($user) {
        $user->full_name = $user->first_name . ' '. $user->last_name;

        return $user;
    });
}
```

**[⬆ back to top](#table-of-contents)**

### Extra `data-` Datatables attributes

[](#extra-data--datatables-attributes)

Datatables [accepts](https://datatables.net/manual/server-side#Returned-data) extra *data-* attributes with each of the record. Following are supported with the package:

1. **DT\_RowId**: This data attribute is added to each of the record by default. You can update `row_id_prefix` config option to set the prefix to that record id.
2. **DT\_RowClass**: You can add `laratablesRowClass()` static method to your model/custom class and return an html class name. Example:

```
/**
 * Specify row class name for datatables.
 *
 * @param \App\User
 * @return string
 */
public static function laratablesRowClass($user)
{
    return $user->is_active ? 'text-success' : 'text-warning';
}
```

**Note** - This method was a regular method in v1.\*.\*

3. **DT\_RowData**: You can add `laratablesRowData()` static method to your model/custom class and return an array with key as the attribute name and its corresponding value. Example:

```
/**
 * Returns the data attribute for url to the edit page of the user.
 *
 * @param \App\User
 * @return array
 */
public static function laratablesRowData($user)
{
    return [
        'edit-url' => route('admin.user.edit', ['user' => $user->id]),
    ];
}
```

**Note** - This method was a regular method in v1.\*.\*

Authors
-------

[](#authors)

- [**Gaurav Makhecha**](https://github.com/gauravmak) - *Initial work*

See also the list of [contributors](https://github.com/freshbitsweb/laratables/graphs/contributors) who participated in this project.

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

Treeware
--------

[](#treeware)

You're free to use this package, but if it makes it to your production environment I would highly appreciate you buying the world a tree.

It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to [plant trees](https://www.bbc.co.uk/news/science-environment-48870920). If you contribute to our forest you’ll be creating employment for local families and restoring wildlife habitats.

You can buy trees at for our forest here [offset.earth/treeware](https://plant.treeware.earth/freshbitsweb/laratables)

Read more about Treeware at [treeware.earth](http://treeware.earth)

Special Thanks to
-----------------

[](#special-thanks-to)

- [Laravel](https://laravel.com) Community

###  Health Score

38

—

LowBetter than 86% of packages

Maintenance41

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 87.8% 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 ~121 days

Recently: every ~489 days

Total

22

Last Release

485d ago

Major Versions

v1.1.7 → v2.0.02019-08-03

PHP version history (3 changes)v1.0.0PHP &gt;=7.0

v2.1.0PHP ^7.2

v4PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/870b0cc126d7dc623c1d4e6045de8ce942236f5dcdebd425bddddaac62b53f90?d=identicon)[angusm73](/maintainers/angusm73)

---

Top Contributors

[![gauravmak](https://avatars.githubusercontent.com/u/11887260?v=4)](https://github.com/gauravmak "gauravmak (144 commits)")[![helissonms](https://avatars.githubusercontent.com/u/20862663?v=4)](https://github.com/helissonms "helissonms (6 commits)")[![SanderMuller](https://avatars.githubusercontent.com/u/9074391?v=4)](https://github.com/SanderMuller "SanderMuller (6 commits)")[![angusm73](https://avatars.githubusercontent.com/u/1692193?v=4)](https://github.com/angusm73 "angusm73 (2 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (2 commits)")[![latheesan-k](https://avatars.githubusercontent.com/u/6196415?v=4)](https://github.com/latheesan-k "latheesan-k (1 commits)")[![liamdemafelix](https://avatars.githubusercontent.com/u/798476?v=4)](https://github.com/liamdemafelix "liamdemafelix (1 commits)")[![CJau](https://avatars.githubusercontent.com/u/25449733?v=4)](https://github.com/CJau "CJau (1 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/angusm73-laratables/health.svg)

```
[![Health](https://phpackages.com/badges/angusm73-laratables/health.svg)](https://phpackages.com/packages/angusm73-laratables)
```

###  Alternatives

[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169175.3k2](/packages/spatie-laravel-mix-preload)[naoray/eloquent-model-analyzer

Helpful methods to gain more information about eloquent model classes.

16429.8k1](/packages/naoray-eloquent-model-analyzer)[frittenkeez/laravel-vouchers

Voucher system for Laravel 9+

5818.8k1](/packages/frittenkeez-laravel-vouchers)[qlick/laravel-full-calendar

Laravel helper for FullCalendar.io. Fork of https://github.com/maddhatter/laravel-fullcalendar

1345.5k](/packages/qlick-laravel-full-calendar)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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