PHPackages                             samvaughton/ldt - 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. [API Development](/categories/api)
4. /
5. samvaughton/ldt

ActiveLibrary[API Development](/categories/api)

samvaughton/ldt
===============

Laravel 4 DataTables library to handle the server-side calls.

0.1.3(12y ago)91.1k1MITPHPPHP &gt;=5.3.0

Since Mar 13Pushed 12y ago1 watchersCompare

[ Source](https://github.com/samvaughton/laravel4-datatables)[ Packagist](https://packagist.org/packages/samvaughton/ldt)[ Docs](https://github.com/samvaughton/laravel4-datatables)[ RSS](/packages/samvaughton-ldt/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (3)Versions (6)Used By (0)

DataTables Library for Laravel 4
================================

[](#datatables-library-for-laravel-4)

[![Build Status](https://camo.githubusercontent.com/f42e95ce25e3cb81c90899d847c1c70a00393f35ba89e1cad32f69cd3bbade81/68747470733a2f2f7472617669732d63692e6f72672f73616d7661756768746f6e2f6c61726176656c342d646174617461626c65732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/samvaughton/laravel4-datatables)

This library is for the server side processing of the client requests. It aims to be different than existing DataTable packages, taking a different approach that allows greater flexibility.

Composer
--------

[](#composer)

```
"require": {
    "samvaughton/ldt": "0.1.*"
}
```

Then run `composer update`.

Simple Example
--------------

[](#simple-example)

```
$customers = \Customer::select('id', 'name', 'email', 'phone', 'date_registered');

$dth = new DataTable(
    new LaravelBuilder($customers),
    new Request(\Input::all()),
    array(
        new Column('id'),
        new Column('name', array('searchable' => true)),
        new Column('email', array('searchable' => true)),
        new Column('phone'),
        new Column('date_registered', array(
            'rowProcessor' => new \DateColumnProcessor()
        )),
        new Column('actions', array(
            'type' => Column::TYPE_STATIC,
            'rowProcessor' => function($value, $row, $originalRow) {
                return sprintf(
                    'Edit',
                    $row['id']
                );
            }
        )
    )
);

return $dth->make();
```

*I have imported the necessary namespace paths via `use` for `DataTable`, `LaravelBuilder`, `Request` and `Column`. `DateColumnProcessor` is a custom class, you can look at an example in the `Column` namespace.*

Quite a lot is going on here, but it is very readable. The `DataTable` class accepts three parameters. A class that implements `BuilderInterface` (there is one already built for Laravel), a `Request` class which handles the parsing of the client side request and thirdly an array of columns which are visible to the user.

The first two parameters will be kept exactly the same 99% of the time.

### Column

[](#column)

The column class accepts two parameters, the first one is required. It can either be a string or an array, this depends on the complexity of the column.

Each column can either be **dynamic** or **static**. A dynamic column is one that originates from the data source. Whereas a static column is appended onto the results after being fetched. Such as an actions column that contains buttons for edit, delete etc..

#### Aliases

[](#aliases)

If you are using aliases in your query and performing searches as well, then you will need to use an array to define the SQL column separately. An example will clear this up.

```
$query = DB::table('customers')->select('customers.name AS customerName');
$dth = new DataTable(
    new LaravelBuilder($customers),
    new Request(\Input::all()),
    array(
        new Column(array('customerName', 'customers.name'), array(
            'searchable' => true
        ))
    )
);
```

The select statement is using an alias, since MySQL cannot utilise aliases within the `WHERE` clause, we have to use its original column name `customers.name`. Otherwise the generated SQL for `WHERE` would look something like:

```
WHERE `customerName` LIKE '%john doe%'

```

Which is illegal.

#### Options

[](#options)

The column class has default options which are listed below and explained, these can be set via the second parameter like the example above.

```
'type'                 => self::TYPE_DYNAMIC
'sortable'             => true
'searchable'           => false
'rowProcessor'         => false
'filterTermProcessor'  => false
'filterQueryProcessor' => false
```

- `type` can be `TYPE_DYNAMIC` or `TYPE_STATIC`.
- `sortable` and `searchable` are booleans (true/false).
- `rowProcessor` is a callback / class that implements the `RowProcessorInterface`.
- `filterTermProcessor` is a callback / class that implements the `FilterTermProcessorInterface`.
- `filterQueryProcessor` is a callback / class that implements the `FilterQueryProcessorInterface`. This is a bit more advanced and lets you define your own SQL to filter against.

#### Processor's

[](#processors)

The `rowProcessor` options allows you to run a function against each column's data, this is for scenarios where you need to append some action buttons or convert a unix timestamp to a more readable date.

```
new Column('actions', array(
    'type' => Column::TYPE_STATIC,
    'rowProcessor' => function($value, $row, $originalRow) {
        return sprintf(
            'Edit',
            $row['id']
        );
    }
)
```

This is a static column that appends an edit button onto every row, it utilises the customers `id` from the `$row` array. You may be wondering what the `$originalRow` is for, this is an untouched row that contains every column from your select statement. Using the above code, if we were to modify the `id` column and set the value to `null` then this processor would return:

```
Edit

```

Due to the `id` column being modified before this one. Here we could utilise `$originalRow['id']` to get the unaltered value.

*If you are trying to use a column that isn't defined within the column array then you have to use `$originalRow` as these columns are not contained within `$row`.*

Instead of passing a callback, you can also pass a class that implements the `RowProcessorInterface`.

```
