Donations Appreciated

There is no exercise better for the heart than reaching down and lifting people up. - John Holmes

Click here to lend your support to: Laravel Datatables and make a donation at pledgie.com !

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.

Requirements

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
              ->eloquent($this->query())
              ->make(true);
      }
      
    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()
              ->columns([
                  'id',
                  'name',
                  'email',
                  'created_at',
                  'updated_at',
              ])
              ->parameters([
                  '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.

    @extends('app')
    
    @section('content')
    {!! $dataTable->table() !!}
    @endsection
    
    @push('scripts')
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.0.3/css/buttons.dataTables.min.css">
    <script src="https://cdn.datatables.net/buttons/1.0.3/js/dataTables.buttons.min.js"></script>
    <script src="/vendor/datatables/buttons.server-side.js"></script>
    {!! $dataTable->scripts() !!}
    @endpush
    
  5. Register our route and test!

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