Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Last active August 30, 2021 01:29
Using Yajra Datatables with Laravel 5.3

Installation

composer require yajra/laravel-datatables-oracle:~6.0

Open up config/app.php and add the following in providers key:

Yajra\Datatables\DatatablesServiceProvider::class,

Then run the following:

php artisan vendor:publish --tag=datatables

Create Dummy Users

$ php artisan tinker
>>> factory(App\User::class, 100)->create();

Create Route

Route::get('/users', 'DatatablesController@index');
Route::any('/data', 'DatatablesController@anyData')->name('datatables.data');

Create View

@extends('layouts.app')

@section('styles')
    <link rel="stylesheet" href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css">
@endsection

@section('scripts')
    <script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
    <script>
    $(function() {
        $('#users-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{!! route('datatables.data') !!}',
            columns: [
                { data: 'id', name: 'id' },
                { data: 'name', name: 'name' },
                { data: 'email', name: 'email' },
                { data: 'created_at', name: 'created_at' },
                { data: 'updated_at', name: 'updated_at' }
            ]
        });
    });
    </script>
@endsection

@section('content')
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-body">
                <table class="table table-bordered" id="users-table">
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Created At</th>
                            <th>Updated At</th>
                        </tr>
                    </thead>                </table>
            </div>
        </div>
    </div>
@stop
@namlao
Copy link

namlao commented Aug 30, 2021

controller ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment