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.

DataTable Console Commands

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.


List Available Commands

php artisan list

Creating a DataTable Service Class

Create a DataTable service class:

php artisan datatables:make Posts

This will create a PostsDataTable class in the app/DataTables directory.


DataTable Class Example

<?php
// app/DataTables/PostsDataTable.php
 
namespace App\DataTables;
 
use App\Models\User;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;
 
class PostsDataTable extends DataTable
{
public function dataTable($query)
{
return $this->datatables
->eloquent($this->query())
->addColumn('action', 'path.to.action.view');
}
 
public function query()
{
$query = User::query();
 
return $this->applyScopes($query);
}
 
public function html()
{
return $this->builder()
->columns($this->getColumns())
->ajax('')
->addAction(['width' => '80px'])
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
]);
}
 
protected function getColumns(): array
{
return [
Column::make('created_at'),
Column::make('updated_at'),
Column::make('id'),
];
}
 
protected function filename(): string
{
return 'posts_' . time();
}
}

Model Option

Pass a --model option to set the model to be used by our DataTable:

php artisan datatables:make Posts --model

This will generate an App\DataTables\PostsDataTable class that uses App\Post as the base model. The exported filename will also be set to posts_(timestamp).


DataTable Class With Model

<?php
// app/DataTables/PostsDataTable.php
 
namespace App\DataTables;
 
use App\Models\Post;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;
 
class PostsDataTable extends DataTable
{
public function dataTable($query)
{
return $this->datatables
->eloquent($this->query())
->addColumn('action', 'path.to.action.view');
}
 
public function query()
{
$query = Post::query();
 
return $this->applyScopes($query);
}
 
public function html()
{
return $this->builder()
->columns($this->getColumns())
->ajax('')
->addAction(['width' => '80px'])
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
]);
}
 
protected function getColumns(): array
{
return [
Column::make('created_at'),
Column::make('updated_at'),
Column::make('id'),
];
}
 
protected function filename(): string
{
return 'posts_' . time();
}
}

Model Namespace Option

php artisan datatables:make Posts --model-namespace="Models\Client"

This implicitly activates the --model option and allows you to use a non-standard namespace.


Action Option

php artisan datatables:make Posts --action="client.action"

Sets a custom path for the action column view.


Columns Option

php artisan datatables:make Posts --columns="id,title,author"

Sets the columns to be used by our DataTable.


Creating a DataTable Scope

DataTable scope is a class that we can use to limit our database search results based on the defined query scopes:

php artisan datatables:scope ActiveUser

This will create an ActiveUser class in app/DataTables/Scopes directory:

<?php
// app/DataTables/Scopes/ActiveUser.php
 
namespace App\DataTables\Scopes;
 
use Yajra\DataTables\Contracts\DataTableScopeContract;
 
class ActiveUser implements DataTableScopeContract
{
/**
* Apply a query scope.
*/
public function apply($query)
{
return $query->where('active', true);
}
}

See Also