Buttons Quick Starter
This guide helps you get started with the DataTables Buttons plugin.
Create Users DataTable
php artisan datatables:make Users
Update UsersDataTable
Update UsersDataTable class and set the columns and parameters needed to render our DataTable:
<?php// app/DataTables/UsersDataTable.php namespace App\DataTables; use App\Models\User;use Yajra\DataTables\Html\Column;use Yajra\DataTables\Html\Button;use Yajra\DataTables\Services\DataTable; class UsersDataTable extends DataTable{ public function html() { return $this->builder() ->columns($this->getColumns()) ->layout([ 'topStart' => 'buttons', 'topEnd' => 'search', 'bottomStart' => 'info', 'bottomEnd' => 'paging', ]) ->buttons([ Button::make('export'), Button::make('print'), Button::make('reset'), Button::make('reload'), ]); } protected function getColumns(): array { return [ Column::make('name'), Column::make('email'), Column::make('created_at'), Column::make('updated_at'), Column::make('id'), ]; }}
Example Route
<?php// routes/web.php use App\DataTables\UsersDataTable;use Illuminate\Support\Facades\Route; Route::get('users', function(UsersDataTable $dataTable) { return $dataTable->render('users.index');});
Example View
Create a view at resources/views/users/index.blade.php:
@extends('layouts.app') @section('content') {!! $dataTable->table() !!}@endsection @push('scripts') {!! $dataTable->scripts() !!}@endpush
See Also
- Buttons Installation - Install buttons plugin
- Buttons Export - Export options
- Buttons Custom - Custom buttons