problem with yajra data tables in laravel

Viewed 1913

I'm trying to implement a datatable into my laravel project, i installed yajra datatables in the project and everything ..

i add these to the app.php :

Yajra\DataTables\DataTablesServiceProvider::class,
'DataTables' => Yajra\DataTables\Facades\DataTables::class,

but when i try to make a new datatable using the command " php artisan datatables:make Companies" i get this error :

There are no commands defined in the "DataTables" namespace.

any idea how to solve this !!

5 Answers

If error:

composer require yajra/laravel-datatables-buttons

You can try:

composer require yajra/laravel-datatables-buttons:*

it works on yajra/laravel/datatables-oracle:9.0

just do the 3 things

  1. run this command "composer require yajra/laravel-datatables-buttons"

  2. paste this two line in providers in config/app.php file: Yajra\DataTables\DataTablesServiceProvider::class, Yajra\DataTables\ButtonsServiceProvider::class,

  3. run this "php artisan vendor:publish --tag=datatables-buttons"

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.

If you already upgraded your Laravel version to Laravel 9

You will be needing the Yajra DataTables version: ^10

composer require yajra/laravel-datatables-oracle:"^10.0"

Modify the app.php

Service Provider & Facade 
Register provider and facade on your config/app.php file.

'providers' => [
    ...,
    Yajra\DataTables\DataTablesServiceProvider::class,
]

'aliases' => [
    ...,
    'DataTables' => Yajra\DataTables\Facades\DataTables::class,
]

Then publish,

php artisan vendor:publish --provider="Yajra\DataTables\DataTablesServiceProvider"

After publishing the DataTablesServiceProvider

Install data tables button 9.0

composer require yajra/laravel-datatables-buttons:9.0

Then run

php artisan datatables:make Companies

Credits to the original author of Yajra / ArJay Angeles

https://github.com/yajra/laravel-datatables

Related