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

routes/web.php
use Yajra\DataTables\Facades\DataTables;
use Yajra\DataTables\Html\Builder;
use Yajra\DataTables\Html\Column;
use App\Models\User;
 
Route::get('users', function(Builder $builder) {
if (request()->ajax()) {
return DataTables::of(User::query())->toJson();
}
 
$html = $builder->columns([
Column::make('id'),
Column::make('name'),
Column::make('email'),
Column::make('created_at'),
Column::make('updated_at'),
]);
 
return view('users.index', compact('html'));
});
resources/views/users/index.blade.php
@extends('app')
 
@section('contents')
{{ $html->table() }}
@endsection
 
@push('scripts')
{{ $html->scripts() }}
@endpush