onlinecode

Laravel 5 – Implementing datatables tutorial using yajra package

Laravel 5 – Implementing datatables tutorial using yajra package

In this post we will give you information about Laravel 5 – Implementing datatables tutorial using yajra package. Hear we will give you detail about Laravel 5 – Implementing datatables tutorial using yajra packageAnd how to use it also give you demo for it if it is necessary.

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' => [

....

YajraDatatablesDatatablesServiceProvider::class,

],

'aliases' => [

....

'Datatables' => 'YajraDatatablesFacadesDatatables',

]

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

Also see:Laravel Google OAuth authentication using Socialite Package

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 IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


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 AppHttpControllers;


use IlluminateHttpRequest;

use Datatables;

use DB;


class PostController extends Controller

{

/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

public function datatable()

{

return view('datatable');

}


/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

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.onlinecode/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.onlinecode/plugin/jquery.js"></script>

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

</head>

<body>


<div >

<table id="users" 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:

Also see:Laravel 5.3 – How to create SEO friendly sluggable URL

http://localhost:8000

Video

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

I hope it can help you…

Hope this code and post will helped you for implement Laravel 5 – Implementing datatables tutorial using yajra package. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version