DataTables has a built-in html builder that you can use to automatically generate your table mark-up and javascripts declarations.
You can use the Builder
class by using Dependency Injection.
use Yajra\DataTables\Html\Builder;
Route::get('users', function(Builder $builder) {
//
});
Route::get('users', function() {
$builder = app('datatables.html');
});
use Yajra\DataTables\DataTables;
Route::get('users', function(DataTables $dataTable) {
$builder = $dataTable->getHtmlBuilder();
});
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'));
});
@extends('app')
@section('contents')
{{ $html->table() }}
@endsection
@push('scripts')
{{ $html->scripts() }}
@endpush