Multiple Laravel DataTables in one view — as a Service Implementation

Hi, If you are new to yajra/laravel-datatables follow the installation or new to Laravel DataTables as a Service Implementation, I recommend you to have a look at it here.
We’ll be rendering 2 data-table in 1 view quick and easy using latest version of yajra/laravel-datatables as a service.

Step 1 — Define Route

Add the below code in routes/web.php file to instantiate the route. Note the extra route dashboard.user and dashboard.notice.

Route::get('/',[App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard.index');

Route::get('user',[App\Http\Controllers\DashboardController::class, 'getUser'])->name('dashboard.user');


Step 2 — Generate and Set Up Controller

Run command to create DashboardController.

php artisan make:controller DashboardController

Add code in app/Http/Controllers/DashboardController.php file. Note the two DataTable Service Class implementation.

Step 3— Generate DataTable Service

Run command to create UserDataTable and NoticeDataTable.

php artisan datatables:make User
php artisan datatables:make Notice

Place the below code in app/DataTables/UserDataTable.php  and app/DataTables/NoticeDataTable.php

Note on line 48, minifiedAjax(route(‘dashboard.user’))? It calls the route defined earlier to fetch user.

Step 4— Setup Blade View

Head over to the resources/views folder and create a dashboard/index.blade.php file and insert the below code in it.


<div class="row">
<div class="table-responsive">
  {{$userDataTable->table(['width' => '100%', 'id'=>'user-data-table', 'class' => 'table table-striped dataTable'])}}
<div class="table-responsive">
  {{$noticeDataTable->table(['width' => '100%', 'id'=>'notice-data-table', 'class' => 'table table-striped dataTable'])}}


And That’s it. Enjoy!