Nasrul Hazim Bin Mohamad

Laravel: Datatable

Feb
18

Assalamualaikum / Hi,

Today I will share how to use Datatables in your Laravel project.

I will cover on:

  1. Setting up Datatables
  2. Compiling Datatables Asset with Laravel Mix
  3. Create route for Datatables
  4. Create reusable Blade Template for Datatable

Part 1: Installation

Create a new laravel project. Install package.json dependencies with npm install or yarn install. Add Datatable and Datatable Bootstrap 4 – yarn add datatables.net datatables.net-bs4 --dev. --dev means that we only need this on development since we will compile it once and used the compiled version for our application.

Open up webpack.mix.js and update the file as following.

Then run npm run prod – this should create public/js/datatables.js and public/css/datatables.css

The next thing you need to install is the Yajra Laravel Datatablescomposer require yajra/laravel-datatables:^1.0 and then run php artisan vendor:publish --tag=datatables.

If you’re running on minimum of Laravel 5.5, thanks to package discovery. No need to setup config/app.php. 🙂

Part 2: Datatable Routes

In this part, I’m going to show how I manage Datatable routes.

The starting point is the app/Providers/RouteServiceProvider.php. Open up the file and we will duplicate one of the method – mapApiRoutes() – and name it as mapDatatableRoutes().

Next, create routes/datatable.php file and we can start write datatable routes in the file. As you can see, with this method, your datatable routes will be much cleaner and organised in a separate route file.

This is just an approach to make your route file easier to maintain. The other option, you may use macro.

Following are the sample datatable route that we will use in this post.

<?php 

Route::get('user', 'UserController')->name('user');

Part 3: Basic Datatable Controller

In this part, I’ll show how I manage the controllers for datatables. As you can see from my mapDatatableRoutes(), I have namespace for the datatable route to App\Http\Controllers\Datatable. This approach just to make your datable controllers well organise.

You can create your first datatable controller with php artisan make:controller Datatable/UserController and you can copy paste the basic setup of the datatable controller.

Part 4: View

In this part, I’ll show how I create basic datatable blade component that reusable – of course, you can use VueJs as well, but I’ll keep it simple for now.

By default, I have this resources/views/components/table.blade.php, which I always use for table based view.

Next, create resources/views/components/datatable.blade.php.

Now we are prepared with the requirements on datatable – Datatable JS & CSS, Laravel Datatable by Yajra, dedicated route and namespace to keep codes well organised. Next, we will test our setup.

Part 5: Final Countdown

We will list out users using datatable. Make sure to run php artisan make:auth and add @stack('styles') and @stack('scripts') in resources/views/layouts/app.blade.php.

Previously you have setup the route and controller. Next is to create a controller, route and a view to display the data from datatable. Following are the setup.

Create the controller – php artisan make:controller UserController -r

Create resources/views/users/index.blade.php

Add route for the view in routes/web.php: Route::get('/users', 'UserController@index')->name('user');

Next (2 more steps!), seed some data with tinker php artisan tinker, then factory(\App\User::class,100)->create();.

And finally, run php artisan serve, the go the browser – http://127.0.0.1/users, you have as the datatable display nicely!

Conclusion

So, as conclusion, the tedious part is to setup the requirement for the datatable. Once you’re done, you will probably will involved with creating route, controller for the datatable and view, and view for the controller view.

You can extend the datatable blade component based on your needs. As for me, for now, the basic setup is good enough.

Here the repository for this post example.

Should I create a package for this?

Thanks! Happy Coding!

One Response to Laravel: Datatable

  1. Please create such a package. This is beautiful!

Leave a Reply

Your email address will not be published. Required fields are marked *

13 − 10 =