4 months ago

How to add export buttons to yajra/laravel-datatables?

Posted 4 months ago by Conixs

Hello everyone! I'm already using it to render my data as datatable, but now I wish to be able to export it on PDF and XLS.

  1. Do I need to add the Buttons Plugin to be able to achieve it?
  2. Do I need to create a new datatable model - php artisan datatables:make - for each table?
public function index(Request $request)
        if($request->ajax()) {
            $model = Shift::withTrashed()->select('shifts.*');
            return $this->dataTable
                ->addColumn('deleted_at', function (Shift $shift) {
                        return '<span class="status--denied">INATIVO</span>';
                    return '<span class="status--process">ATIVO</span>';
                ->addColumn('edit', function (Shift $shift) {
                    return '<div class="table-data-feature"><a href="'.route('shifts.edit', ['shift' => $shift]).'" class="item" data-toggle="tooltip" data-placement="top" data-original-title="Editar"><i class="zmdi zmdi-edit"></i></a></div>';
                ->rawColumns(['deleted_at', 'edit'])

        $shifts = Shift::withTrashed()->get();
        return view('shifts.index', ['shifts' => $shifts]);
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript"

<script type="text/javascript">
    $(document).ready(function () {
        var table = $('.table-data2').DataTable({
            serverSide: true,
            "pageLength": 5,
            dom: 'frt',
            ajax: '{{route('shifts.index')}}',
            columns: [
                {data: 'name', name: 'name'},
                {data: 'deleted_at', name: 'deleted_at', 'searchable': false},
                {data: 'edit', name:'edit', 'searchable': false, 'orderable': false}
            drawCallback: function () {
                if ($('.au-btn-load').is(':visible')) {
                    $('html, body').animate({
                        scrollTop: $('.au-btn-load').offset().top
                    }, 1000);


        $('.au-btn-load').on('click', function () {

Please sign in or create an account to participate in this conversation.