Laravel Example

DataTable 8 as service with Laravel 5.5

jQuery Datatable is the most famous plugin. Today I am going to show you that how you can implement jQuery Yajra Datatable as service in Laravel 5.5. 


The benefits of service approach that are:

-Built-in support for DataTables Buttons (server-side processing).

-Smaller footprint for our controller.

-Artisan command available for creating our service.


Before moving on actual code, you need to install Laravel 5.5. Make sure that your PHP version is 7 or later or that.

After installing Laravel 5.5 You need to install this package and run composer update command.

// composer.json 
"require": {
    "yajra/laravel-datatables-buttons": "^3.0",
    "yajra/laravel-datatables-html": "^3.1",
    "yajra/laravel-datatables-oracle": "~8.0"


Update your composer.json file and then run composer update command. After successful installation, you need to load service provider and add facade into config/app.php file.

'providers' => [

'aliases' => [
    'DataTables' => Yajra\DataTables\Facades\DataTables::class,

Now you are ready to create service or controller file and then just inject Builder class. Here I created UserController.php and register my routes with web.php. Last is to create new blade file.

The first step is to register web-routes. Here is my web.php into Laravel 5.5:

Route::get('users', 'UserController@index')->name('users.index');

 Here is my controller file app/Http/Controllers/UserController.php

namespace App\Http\Controllers;

use App\User;
use Yajra\DataTables\Html\Builder;
use Illuminate\Http\Request;

class UserController extends Controller{

    public function index(Request $request, Builder $htmlBuilder) {
        if ($request->ajax()) {
            return datatables()->eloquent($this->query())->make(true);

        $dataTable = $htmlBuilder->columns($this->getColumns());
        return view('users.index', compact('dataTable'));

    public function query() {
        $query = User::query();
        return $query;

    public function getColumns() {
        $return_value = [
            'id'           => ['title' => 'ID'],
            'name'         => ['title' => 'Name'],
            'email'        => ['title' => 'Email'],
        return $return_value;

Of course, you can create new service file and inject it with this controller. Hope you like it for more information click here


Laravel5.5 Datatable Service
Tutorial By Gautam Patadiya