2017 © Pedro Peláez
 

library eloquent-datatable

Eloquent DataTable plugin for server side ajax call handling.

image

livecontrol/eloquent-datatable

Eloquent DataTable plugin for server side ajax call handling.

  • Wednesday, July 4, 2018
  • by jeffreydevreede
  • Repository
  • 9 Watchers
  • 48 Stars
  • 170,440 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 20 Forks
  • 6 Open issues
  • 5 Versions
  • 5 % Grown

The README.md

EloquentDataTable Build status

Eloquent compatible DataTable plugin for server side ajax call handling., (*1)

If you are familiar with eloquent and would like to use it in combination with datatables this is what you are looking for., (*2)

Usage

Step 1: Install through composer

composer require livecontrol/eloquent-datatable, (*3)

Step 2: Add DataTables javascript and set it up

For more information check out the datatables manual. Make sure you have csrf requests working with jquery ajax calls., (*4)

var table = $('#example').DataTable({
  "processing": true,
  "serverSide": true,
  "ajax": {
    "url": "<url to datatable route>",
    "type": "POST"
  }
});

Step 3: Use it

$users = new Models\User();
$dataTable = new LiveControl\EloquentDataTable\DataTable($users, ['email', 'firstname', 'lastname']);
echo json_encode($dataTable->make());

Optional step 4: Set versions of DataTables plugin.

Just initialize the DataTable object as you would normally and call the setVersionTransformer function as in the following example (for version 1.09 of DataTables):, (*5)

$dataTable->setVersionTransformer(new LiveControl\EloquentDataTable\VersionTransformers\Version109Transformer());

By default the plugin will be loading the transformer which is compatible with DataTables version 1.10., (*6)

Examples

Basic example

use LiveControl\EloquentDataTable\DataTable;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable(
      $users->where('city', '=', 'London'),
      ['email', 'firstname', 'lastname']
    );

    return $dataTable->make();
  }
}

In this case we are making a datatable response with all users who live in London., (*7)

Combining columns

If you want to combine the firstname and lastname into one column, you can wrap them into an array., (*8)

use LiveControl\EloquentDataTable\DataTable;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable(
      $users,
      ['email', ['firstname', 'lastname'], 'city']
    );

    return $dataTable->make();
  }
}

Using raw column queries

Sometimes you want to use custom sql statements on a column to get specific results, this can be achieved using the ExpressionWithName class., (*9)

use LiveControl\EloquentDataTable\DataTable;
use LiveControl\EloquentDataTable\ExpressionWithName;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable(
      $users,
      [
        'email',
        new ExpressionWithName('`id` + 1000', 'idPlus1000'),
        'city'
      ]
    );

    return $dataTable->make();
  }
}

Return custom row format

If you would like to return a custom row format you can do this by adding an anonymous function as an extra argument to the make method., (*10)

use LiveControl\EloquentDataTable\DataTable;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable($users, ['id', ['firstname', 'lastname'], 'email', 'city']);

    $dataTable->setFormatRowFunction(function ($user) {
      return [
        $user->id,
        '<a href="/users/' . $user->id . '">' . $user->firstnameLastname . '</a>',
        '<a href="mailto:' . $user->email . '">' . $user->email . '</a>',
        $user->city,
        '<a href="/users/delete/' . $user->id . '">&times;</a>'
      ];
    });

    return $dataTable->make();
  }
}

Showing results with relations

use LiveControl\EloquentDataTable\DataTable;

class UserController {
  ...
  public function datatable()
  {
    $users = new User();
    $dataTable = new DataTable(
        $users->with('country'),
        ['name', 'country_id', 'email', 'id']
    );

    $dataTable->setFormatRowFunction(function ($user) {
        return [
            '<a href="/users/'.$user->id.'">'.$user->name.'</a>',
            $user->country->name,
            '<a href="mailto:'.$user->email.'">'.$user->email.'</a>',
            '<a href="/users/delete/'.$user->id.'">&times;</a>'
        ];
    });

    return $dataTable->make();
  }
}

The Versions

04/07 2018

dev-master

9999999-dev

Eloquent DataTable plugin for server side ajax call handling.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jeffrey de Vreede

laravel eloquent datatable

15/09 2015

0.1.5

0.1.5.0

Eloquent DataTable plugin for server side ajax call handling.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jeffrey de Vreede

laravel eloquent datatable

17/04 2015

0.1.4

0.1.4.0

Eloquent DataTable plugin for server side ajax call handling.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jeffrey de Vreede

laravel eloquent datatable

14/04 2015

0.1.3

0.1.3.0

Eloquent DataTable plugin for server side ajax call handling.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jeffrey de Vreede

laravel eloquent datatable

12/04 2015

0.1.2

0.1.2.0

Eloquent DataTable plugin for server side ajax call handling.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jeffrey de Vreede

eloquent datatable