Laravel Datatables | Yajra Datatables for Laravel 7

Hello Artisan 

In this tutorial, i am going to share with you how to install and use datatables in laravel 7 application. In this laravel demo datatable tutorial i will use datatables laravel datatables plugin. I will write step be step tutorial for laravel 7 datatables ajax.

Datatables provides you quick search, ordering, sorting, pagination etc. Datatables is jQuery plugins that allows us adding some advanced interaction controls to your HTML tables data. Datatables also provide ajax for data searching and getting. you can give very quick layout for search and sorting using Datatables. You can also implement Datatables in your laravel application.

In this yajra datatables for laravel 7 tutorial i will use users table. So we don't need to create other Model and controller. I will simply show you how we can add laravel yajra datable in our laravel application.

Table of contents

  • Download Fresh Laravel 7 App
  •  Install Yajra Datatable Package
  • Add Some Dummy Data
  • Create Route
  • Create Blade To View Datatable

You have to just follow some step to implement datatables in your laravel 7 application. To add datatable in our laravel app i will use Yajra Datatable Package. So let's start laravel yajra datatable tutorial from scratch.

How-to-Install-Yajra-DataTable-In-Laravel-7

Step 1: Install Laravel 7

In this step we have to download fresh laravel app to create step by step laravel yajra datatable example. So download it by following command.

composer create-project --prefer-dist laravel/laravel datatable

 

Step 2 : Install Yajra Datatable Package

Now we have to install yajra datatable package in our laravel app. So run below command to install it.

composer require yajra/laravel-datatables-oracle

After that you need to add providers and alias in the following path.

config/app.php

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

 

Step 3:  Add Dummy Records

In this we need to add some dummy records to show datatable. I will create user list to show it in datatable. I will use tinker to add it quicly. 

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

 

Step 4:  Create Route

In this step we have to setup our route. I won't use any controller to create and show our users. I will simply return user from route for demo purpose. So follow the below guide to create laravel datatable.

routes/web.php

//Datatable Route
Route::get('users', function(){
  return view('test');
});

Route::get('userslist', function(){
  return datatables()->of(\DB::table('users')->select('*'))
      ->make(true);
})->name('userslist');

 

Step 5: Create Blade To View Datatable

Now in this final step we need to create blade to view datatable. So create test.blade.php and add this following code it it.

resources/views/test.blade.php

 

Read also : Dynamic Pie Charts Example Using Google Charts API in Laravel 7

Now run php artisan serve command and visit this below path then you will see the below datatable.

url
http://localhost:8000/users

 

And see the preview of this laravel yajra datatable tutorial's output.

laravel-datatable-example

In this article, We have successfully installed & now know how to use and install yajra datatables in the laravel 7 Application. Hope it can help you.