Skip to content
Icon

WARNING You're browsing the documentation for an old version of LARAVEL-DATATABLES. Consider upgrading your project to laravel-datatables 11.0.

Html Builder

DataTables has a built-in html builder that you can use to automatically generate your table mark-up and javascripts declarations.

Html Builder via Dependency Injection

You can use the Builder class by using Dependency Injection.

use Yajra\DataTables\Html\Builder;
 
Route::get('users', function(Builder $builder) {
});

Html Builder via IoC

Route::get('users', function() {
$builder = app('datatables.html');
});

Html Builder from DataTables instance

use Yajra\DataTables\DataTables;
 
Route::get('users', function(DataTables $dataTable) {
$builder = $dataTable->getHtmlBuilder();
});

Html Builder Example

use DataTables;
use Yajra\DataTables\Html\Builder;
 
Route::get('users', function(Builder $builder) {
if (request()->ajax()) {
return DataTables::of(User::query())->toJson();
}
 
$html = $builder->columns([
['data' => 'id', 'name' => 'id', 'title' => 'Id'],
['data' => 'name', 'name' => 'name', 'title' => 'Name'],
['data' => 'email', 'name' => 'email', 'title' => 'Email'],
['data' => 'created_at', 'name' => 'created_at', 'title' => 'Created At'],
['data' => 'updated_at', 'name' => 'updated_at', 'title' => 'Updated At']),
]);
 
return view('users.index', compact('html'));
});

On your resources/views/users/index.blade.php.

@extends('app')
 
@section('contents')
{!! $html->table() !!}
@endsection
 
@push('scripts')
{!! $html->scripts() !!}
@endpush