Skip to content
Icon

WARNING You're browsing the documentation for an upcoming version of Laravel DataTables. The documentation and features of this release are subject to change.

HTML Builder Macro

The macro() method allows you to extend the HTML Builder with custom functionality.


Basic Usage

<?php
// app/Providers/AppServiceProvider.php
 
use Yajra\DataTables\Html\Builder;
use Yajra\DataTables\Html\Column;
 
Builder::macro('addEditColumn', function () {
$attributes = [
'title' => 'Edit',
'data' => 'edit',
'name' => '',
'orderable' => false,
'searchable' => false,
];
 
$this->collection->push(new Column($attributes));
 
return $this;
});

Using the Macro

use Yajra\DataTables\Html\Builder;
 
$html = $builder->addEditColumn()->ajax(route('users.data'));

Creating Custom Macros

Action Buttons Macro

use Yajra\DataTables\Html\Builder;
use Yajra\DataTables\Html\Column;
 
Builder::macro('addActions', function () {
$this->collection->push(new Column([
'data' => 'action',
'name' => 'action',
'title' => 'Actions',
'orderable' => false,
'searchable' => false,
'exportable' => false,
'render' => 'function() { return renderActionButtons(data); }',
]));
 
return $this;
});

Status Column Macro

Builder::macro('addStatusColumn', function ($title = 'Status') {
$this->collection->push(new Column([
'data' => 'status',
'name' => 'status',
'title' => $title,
]));
 
return $this;
});

See Also