SigalZ's avatar

Yajra datatables questions

Hello,

I installed yajra datatables version 12 on my laravel 12 project.

I have a few questions and I just don't understand the documentation.

  1. On the Quick Starter instructions, it says:

"Next, we will install Laravel DataTables Vite to simplify our frontend setup.

npm i laravel-datatables-vite --save-dev

This will install the following packages:

  • Bootstrap Icons
  • DataTables with Buttons and Select plugins for Bootstrap 5
  • Laravel DataTables custom scripts

Once installed, we can now configure our scripts and css needed for our application. resources/js/app.js

import './bootstrap'; import 'laravel-datatables-vite';

resources/sass/app.scss

// Fonts @import url('https://fonts.bunny.net/css?family=Nunito');

// Variables @import 'variables';

// Bootstrap @import 'bootstrap/scss/bootstrap';

// DataTables

@import 'bootstrap-icons/font/bootstrap-icons.css'; // This does not exist

@import "datatables.net-bs5/css/dataTables.bootstrap5.min.css";

@import "datatables.net-buttons-bs5/css/buttons.bootstrap5.min.css";

@import 'datatables.net-select-bs5/css/select.bootstrap5.css';

There is no folder called: bootstrap-icons anywhere. What am I missing?

  1. How do I show a relationship value?

e.g. I have a table called green_beans, in the table there is a column: country_id which is a relationship to countries table. I need to display the country's name.

 public function getColumns(): array
    {
        return [
          
            Column::make('id'),
            Column::make('country_id'), // should be country name from countries table.
            Column::make('name'),
            Column::make('stock'),
              Column::computed('action')
                ->exportable(false)
                ->printable(false)
                ->width(60)
                ->addClass('text-center'),
        ];
    }
  1. How do I add an Edit button on each row that will open the Edit page for that green bean. How do I get the green bean id here?
 public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addColumn('action', "<a href='greenbean/'" . green_bean->id // what do I put here? . " class='btn btn-sm btn-primary'>Edit</a>")
            ->setRowId('id');
    }
  1. Why don't I see any of these buttons (excel, csv, pdf, etc.):
 public function html(): HtmlBuilder
    {
        return $this->builder()
            ->setTableId('greenbeans-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->orderBy(1)
            ->selectStyleSingle()
            ->buttons([
                Button::make('excel'),
                Button::make('csv'),
                Button::make('pdf'),
                Button::make('print'),
                Button::make('reset'),
                Button::make('reload')
            ]);
    }

The full code:

In the controller:

 public function index(GreenBeansDataTable $dataTable)
    {       
        return $dataTable->render('admin.green-beans.index');        
    }

GreenBeanDataTable:

Please help.

1 like
13 replies
vincent15000's avatar

Ok I will have a look this week ... I tell you something soon.

SigalZ's avatar
Level 5

Thank you :) I also tried a livewire package: https://github.com/restu-lomboe/datatable-livewire, couldn't make it work either.

Posted on the git issues.

I just found a tutorial on youtube, it looks like they are building the tabels themselves with Livewire 4, going to watch this, just don't want you to waste your time in case this one works for me.

1 like
DigitalArtisan's avatar
There is no folder called: bootstrap-icons anywhere. What am I missing?

It is installed in ~/node_modules folder.

1 like
DigitalArtisan's avatar

Add this ->dom('Bfrtip') to your function html(): HtmlBuilder:


    public function html(): HtmlBuilder
    {
        
        return $this->builder()
                    ->setTableId('users-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->dom('Bfrtip')
                    ->buttons([
                        Button::make('add'),
                        Button::make('excel'),
                        Button::make('csv'),
                        Button::make('pdf'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload'),
                    ]);
    }

dom('Bfrtip') works because it tells DataTables exactly where to place the UI elements, including the Buttons (B). Without it, the newer DataTables layout system (used with Yajra v12 and modern DataTables in Laravel 12/13 setups) may not create a default spot for the buttons, so they are initialized in JavaScript but never rendered in the page.

1 like
vincent15000's avatar

@sigalz I just checked and all works fine. You just have to install the right plugins and declare what you need.

There isn't any folder called boostrap-icons ?

You need to install some bootstrap packages.

composer require laravel/ui --dev
php artisan ui bootstrap

npm i laravel-datatables-vite --save-dev

How to show a relationship ?

public function query(User $model): QueryBuilder
{
    return $model->newQuery()->with('house'); // HERE RELATION EAGER LOADING
}

public function getColumns(): array
{
    return [
        Column::computed('action')
                ->exportable(false)
                ->printable(false)
                ->width(60)
                ->addClass('text-center'),
        Column::make('id'),
        Column::make('house.name')->title('House'), // HERE ADD YOUR COLUMN
        // Column::make('add your columns'),
        Column::make('created_at'),
        Column::make('updated_at'),
    ];
}

How to add an Edit button ?

public function dataTable(QueryBuilder $query): EloquentDataTable
{
    return (new EloquentDataTable($query))
        ->addColumn('action', function (User $user) {
            return '<a href="'.route('users.edit', $user->id).'" class="btn btn-sm btn-primary">Edit</a>';
        })
        ->rawColumns(['action'])
        // ->addColumn('action', 'users.action')
        ->setRowId('id');
}

How to show the export buttons ?

You first need to install de buttons plugin.

composer require yajra/laravel-datatables-buttons php artisan vendor:publish --tag=datatables-buttons

Hope it will help you ;).

vincent15000's avatar

It's working with both versions.

UPDATED => It works fine with both versions 12 and 13.

DigitalArtisan's avatar

The OP stated Laravel 12. Laravel 13 works fine.

For Laravel 12 you need ->dom('Bfrtip') as I mentioned in my earlier post.

The buttons are initialized in JavaScript but never rendered in the page is the root of the problem.

1 like
vincent15000's avatar

I just checked my code with Laravel 12 and Yajra 12 and it works fine.

So it works with Laravel 12 and 13 without any problem.

1 like
jlrdw's avatar

It is easy to build your own table with in place editing, add on the fly, etc.

But first time there is a learning curve, but worth it.

1 like

Please or to participate in this conversation.