Donations Appreciated

It's not how much we give but how much love we put into giving. - Mother Teresa

DataTables as a Service Implementation

Why use this approach?

Some of the reasons are:

  • Built-in support for DataTables Buttons (server-side processing).
  • Smaller footprint for our controller.
  • Artisan command available for creating our service.
  • etc...

Note: This tutorial assumes that you are already familiar with the basic setup/usage of the package. If not, it is recommended that you check Quick Start tutorial first.


After package installation, don't forget to execute: php artisan vendor:publish --tag=datatables to publish the assets.

For datatables, these files will be published:

  1. public/vendor/datatables/buttons.server-side.js
  2. resources/views/vendor/datatables/print.blade.php
  3. config/datatables.php

UPDATE: Laravel 5.4 Upgrade Guide

This tutorial is only applicable to Laravel v5.0 - v5.3. If you are using Laravel 5.4, please see upgrade guide.

Displaying users data by using DataTable Service

  1. Let's start by creating our UsersDataTable

    php artisan datatables:make UsersDataTable

    This will create a file: App\DataTables\UsersDataTable class with the following methods:

    1. ajax() - which will handle the ajax response to our datatable
    2. query() - which will be used to build our base query object.
    3. html() - an optional method that we can use if we want to utilize the Html\Builder of Datatables.

  2. Build UsersDataTable class

    1. Build ajax() method.

      The default stub for ajax method is already usable and we can already leave this as is.

      public function ajax()
          return $this->datatables
    2. Build query() method.

      We will also use the default stub for this method since we are dealing with User model.

      public function query()
          $users = User::select();
          return $this->applyScopes($users);
    3. Build html() method.

      We will add the columns that we want to display on our datatable. We will also include the buttons and display it in dom.

      public function html()
          return $this->builder()
                  'dom' => 'Bfrtip',
                  'buttons' => ['csv', 'excel', 'pdf', 'print', 'reset', 'reload'],
  3. Let's create our controller

    php artisan make:controller UsersController --plain

    We will add the index method and inject our UsersDataTable class. Afterwards, we will render our view using our service.

    namespace App\Http\Controllers;
    use App\DataTables\UsersDataTable;
    use App\Http\Requests;
    class UsersController extends Controller
        public function index(UsersDataTable $dataTable)
            return $dataTable->render('users');
  4. Let's create our view for our datatables.

    Create a file on resources/views/users.blade.php

    Note: buttons.server-side.js is included in the package.

    {!! $dataTable->table() !!}
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src="/vendor/datatables/buttons.server-side.js"></script>
    {!! $dataTable->scripts() !!}
  5. Register our route and test!

    Route::resource('users', 'UsersController');
    Expected output: