Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mstdmstd's avatar

Using laravel-datatables in laravel I see only header of table

Reading manual https://yajrabox.com/docs/laravel-datatables/master/quick-starter

I try to add laravel-datatables to my laravel 8 app and I have in the control:

    public function index(UsersDataTable $dataTable)
    {
        $viewParamsArray = getAppParameters(true, ['csrf_token'], []);
        return $dataTable->render('admin.users.index', $viewParamsArray);
    }

In app/DataTables/UsersDataTable.php :

<?php

namespace App\DataTables;

use App\Models\User;
use Yajra\DataTables\DataTables;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;

class UsersDataTable extends DataTable
{
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('action', 'users.action');
    }

    public function query(User $model)
    {
        \Log::info( '-10 query ::' . print_r( -10, true  ) ); // I do not see this line in log

        return $model->newQuery()->orderBy('users.id', 'asc')   ->map
            (function ($userItem) {
            $userItem->slashed_name = addslashes($userItem->name);
            $permissionsLabel       = '';
                \Log::info(  varDump($userItem->getPermissionNames(), ' -000 $userItem->getPermissionNames()::') );
            foreach ($userItem->getPermissionNames() as $v) {
                $permissionsLabel .= $v . ', ';
            }
            $userItem->permissions_label = trimRightSubString(trim($permissionsLabel), ',');

            return $userItem;
        })
        ->all();
    }

    public function html()
    {
        \Log::info( '-1  public function html( ::' . print_r( -3, true  ) );
        // I see this line in log
        return $this->builder()
                    ->setTableId('users-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->dom('Bfrtip')
                    ->orderBy(1)
                    ->buttons(
                        Button::make('create'),
                        Button::make('export'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload')
                    );
    }

    protected function getColumns()
    {
        return [
            'id',
            'name',
            'email',
            'phone',
        ];
    }

    protected function filename()
    {
        return 'Users_' . date('YmdHis');
    }
}

In resources/views/admin/users/index.blade.php :

@extends($current_admin_template.'.layouts.app')

@section('content')
<div class="backend_listing_container">
    <div class="card col-sm-12">
        <div class="card-header">
            ...
        </div>

        <div class="card-body data_listing_bordered_block">

            @include($current_admin_template.'.layouts.page_header')

            <div class="table-responsive dataTables_header">
                <table class="table table-bordered table-striped text-primary" id="users-table">
                    {{$dataTable->table()}}
                </table>
            </div>

        </div> <!-- card-body -->

    </div>  <!-- card -->
    {{$dataTable->scripts()}}

</div>

@endsection

As result I see only header of table with id="users-table" and no sql-tracement to db. What did miss to make data uploaded on page load and how better to make as I need custome filters form(name text, status/group selection input) to reload data by clicking on “Search” button?

Thnaks!

0 likes
1 reply
mubahood360@gmail.com's avatar

SOLUTION 1 Look for this file in your project

vendor/datatables/buttons.server-side.js

The copy it and put it in your assets public folder and then make sure you include it as a scribe before

{{ $dataTable->scripts() }}

so, it should be like this

<script src="{{ url('/') }}/assets/js/buttons.server-side.js"></script>
{{ $dataTable->scripts() }}

and hopefully it will be fixed.

SOLUTION 2 Remove buttons. On your build method in the datatable class, remove buttons by adding something like this

 ->parameters([
        'dom' => 'Bfrtip',
        'buttons' => [],
    ]);

So, the final thing should look like this

public function html()
{
    return $this->builder()
                ->setTableId('users-table')
                ->columns($this->getColumns())
                ->minifiedAjax()
                ->dom('Bfrtip')
                ->orderBy(1)
                -->parameters([
                    'dom' => 'Bfrtip',
                    'buttons' => [],
                ]);
}

CONCLUSION If you still get an error, go to inspect and see if there is any warning. All the best!

Please or to participate in this conversation.