Laravel 5 - Implementing datatables tutorial using yajra package

By Hardik Savani November 5, 2023 Category : PHP Laravel Bootstrap

Today, I am going to share with you How to use datatables in your laravel 5 application. In this example you can datatables using yajra/laravel-datatables-oracle package.

Datatables provides us quick search, pagination, ordering, sorting and etc. Datatables is basically jQuery plugins that allows you to add 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.

You have to just follow few step for implement datatables in your laravel application. In this example i give you example from scratch. So just follow bellow step, you will find preview and also demo for check how it is working.

Preview:

Step 1: Install Laravel 5.3 Application

In this step, if you haven't laravel 5.3 application setup then we have to get fresh laravel 5.3 application. So run bellow command and get clean fresh laravel 5.3 application.

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

Step 2: Install Package

In this step we have to add yajra/laravel-datatables-oracle package for datatables so one your cmd or terminal and fire bellow command:

composer require yajra/laravel-datatables-oracle

After successfully install package, open config/app.php file and add service provider and alias.

config/app.php

'providers' => [

....

Yajra\Datatables\DatatablesServiceProvider::class,

],

'aliases' => [

....

'Datatables' => 'Yajra\Datatables\Facades\Datatables',

]

You can also make public configuration file by following command, after run this command you will find config/datatables.php file. So run bellow command:

php artisan vendor:publish --tag=datatables

Step 3: Create demo_posts Table

In this step we have to create migration for posts table using Laravel 5.3 php artisan command, so first fire bellow command:

php artisan make:migration create_demo_posts_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;


class CreateDemoPostsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('demo_posts', function (Blueprint $table) {

$table->increments('id');

$table->string('title');

$table->string('category');

$table->string('tag');

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::drop("demo_posts");

}

}

After create migration successfully, we have to run this migration by following command:

php artisan migrate

Step 4: Create Route

In this is step we need to create route for datatables layout file and another one for getting data. so open your routes/web.php file and add following route.

routes/web.php

Route::get('datatable', ['uses'=>'PostController@datatable']);

Route::get('datatable/getposts', ['as'=>'datatable.getposts','uses'=>'PostController@getPosts']);

Step 5: Create Controller

In this point, now we should create new controller as PostController in this path app/Http/Controllers/PostController.php. this controller will manage layout and getting data request and return response, so put bellow content in controller file:

app/Http/Controllers/PostController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use Datatables;

use DB;


class PostController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function datatable()

{

return view('datatable');

}


/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function getPosts()

{

$users = DB::table('demo_posts')->select('*');

return Datatables::of($users)

->make(true);

}

}

Step 6: Create View

In Last step, let's create datatable.blade.php(resources/views/datatable.blade.php) for layout and we will write design code here and put following code:

resources/views/datatable.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5 - Implementing datatables tutorial using yajra package</title>

<link rel="stylesheet" href="http://demo.itsolutionstuff.com/plugin/bootstrap-3.min.css">

<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet">

<script src="http://demo.itsolutionstuff.com/plugin/jquery.js"></script>

<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>

</head>

<body>


<div class="container">

<table id="users" class="table table-hover table-condensed" style="width:100%">

<thead>

<tr>

<th>Id</th>

<th>Post Title</th>

<th>Category</th>

<th>Tag</th>

</tr>

</thead>

</table>

</div>


<script type="text/javascript">

$(document).ready(function() {

oTable = $('#users').DataTable({

"processing": true,

"serverSide": true,

"ajax": "{{ route('datatable.getposts') }}",

"columns": [

{data: 'id', name: 'id'},

{data: 'title', name: 'title'},

{data: 'category', name: 'category'},

{data: 'tag', name: 'tag'}

]

});

});

</script>

</body>

</html>


Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow url on your browser:

http://localhost:8000

Video

You can get more information about package from here : Click Here.

I hope it can help you...

Shares