Today I want to offer you an overview of a package made for DataTables.net integration into Laravel: laravel-datatables. This package allows you to easily create server-side processed DataTables with most of its available functionality while only writing a few lines of code.
Laravel-datatables installation
1. composer.json
"yajra/laravel-datatables-oracle": "~6.0"
2. config/app.php
Yajra\Datatables\DatatablesServiceProvider::class,
Facade should be automatically registered.
3. publishing configuration
$ php artisan vendor:publish --tag=datatables
Some simple magic
As an example I’m going to use our default Users.
Let’s take a simple view with a table:
<!DOCTYPE html> <head> <link href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css" rel="stylesheet"> </head> <body> <table class="datatable"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Created at</th> </tr> </thead> <tbody> </tbody> </table> <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> <script src="//cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script> <script> $(document).ready(function(){ $('.datatable').DataTable({ processing: true, serverSide: true, ajax: '{{ route('users.serverSide') }}' }); }); </script> </body>
And here is our logic to get the main table up and running: (*this code was written directly into routes.php and it is no different than controller code)
<?php use Yajra\Datatables\Datatables; Route::get('/', function () { return view('welcome'); }); Route::get('/users/serverSide', [ 'as' => 'users.serverSide', 'uses' => function () { $users = App\User::select(['id', 'name', 'email', 'created_at']); return Datatables::of($users)->make(); } ]);
After entering into the page we should see a fully working datatable.
This package works great with Eloquent and even Query Builder or Collections. Both of them have a different availability of functions but the main ones are:
- Object data sourcing – you can easily map what data goes to what column
- Row details – to get some more information, such as additional relationships
- Master details – in my opinion, the best way to display child elements (you can have a table for each row of users)
- Data counting – when you really need to know how many times that user logged in
- Specific column searching – should be self explanatory (you can even have it search using POST)
- Add, edit or remove a column – when you need that quick edit the most
- Additional parameters and filters – when you want to have a red border if user is inactive
- Row number – if you need to see which row number that entry has
- Global filter overriding – to get it working differently just for this table (or an advanced version of it)
- Working with dates – we are humans with different timezones
- Eager loading – too bad it does not support search and sort yet…
- Has many relationship – to make life easier
- Joined query
- IOC support
- And many more which could be found here
As you can see, this package has a lot to offer and I couldn’t write everything down here (please visit their official site below). Of course, it can’t fit all of our needs but this is a really great package which is a must when working with datatables.
Keep in mind that there are some requirements involved in this package:
- PHP 5.5.9 or later.
- Laravel 5.0 or later.
As always, don’t forget to read the official documentation.
I have a reservation…..how do i attach actions as an extra column?
Hello,
Please check this documentation page: http://datatables.yajrabox.com/eloquent/add-edit-remove-column
They have added a new Column Action, where they pass html code via return.
Hope this will help you!
how to create another datatable by clicking on specific field of one datatable
All the time when I deal with a matter your website help me .exactly you are awesome
Hope you be successful
Thanks a lot:)
Hi wen I make php artisan vendor:publish –tag=datatables
i have this result:
Nothing to publish for tag [datatables].
Author of this package configure columns, buttons and any html right in controllers. That’s wrong