Laravel 9 Yajra Datatables Export Excel CSV Button Example

Hello Artisan,

In this Laravel tutorial, I will show you how to add export to csv and excel button in yajra datatable. That mean from this tutorial, you will also learn how we can customize our yajra datatable.

You know that export to csv, export excel and pdf or print button is a common thing of a datatable. From this example, you will learn laravel datatables export excel and csv. you can see laravel datatables export csv and excel with example.

If you want to see an example of how to add an export button in yajra datatable then you are in the right place. We will see from this tutorial that yajra laravel datatables export to excel csv.

 

Step 1: Install Laravel

In this step, we need a fresh Laravel application to get started. So download a fresh laravel application.

composer create-project --prefer-dist laravel/laravel blog

 

Step 2 : Install Yajra Datatable

In this step, we have to install yajra datatable composer package for datatable and yajra/laravel-datatables-buttons export buttons, so you can install using the following command:

composer require yajra/laravel-datatables-oracle
composer require yajra/laravel-datatables-buttons

 

After that, you need to set providers and alias.

config/app.php

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

 

Now you have to run the below command to get the configuration file, so let's run the below command:

php artisan vendor:publish --tag=datatables-buttons

 

Read also: Laravel React Js Pagination Example From Scratch

 

Step 3: Add Dummy Records

In this step, we will create some dummy users using the tinker factory. so let's create dummy records using below command:

php artisan tinker

factory(App\User::class, 200)->create();

 

Step 4: Create DataTable Clas

Here, we need to create a User DataTable class using the Yajra Datatable command. so let's run below command:

php artisan datatables:make Users

 

app/DataTables/UsersDataTable.php

namespace App\DataTables;
  
use App\User;
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
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query);
    }
  
    /**
     * Get query source of dataTable.
     *
     * @param \App\User $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(User $model)
    {
        return $model->newQuery();
    }
  
    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
                    ->setTableId('users-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->orderBy(1)
                    ->parameters([
                        'dom'          => 'Bfrtip',
                        'buttons'      => ['excel', 'csv'],
                    ]);
    }
  
    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::make('id'),
            Column::make('name'),
            Column::make('email'),
        ];
    }
  
    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'Users_' . date('YmdHis');
    }
}

 

Step 5: Add Route

In this step, we need to create a route for the data tables layout file and another one for getting data. so open your routes/web.php file and add the following route.

routes/web.php

Route::get('users', 'UserController@index');

 

Step 6: Create Controller

At this point, now we should create a new controller as UserController. this controller will manage layout and get data requests and return responses, so put below content in the controller file:

app/Http/Controllers/UserController.php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\DataTables\UsersDataTable;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(UsersDataTable $dataTable)
    {
        return $dataTable->render('users');
    }
}

 

Step 7: Create View

In the last step, let's create users.blade.php(resources/views/users.blade.php) for the layout and we will write design code here and put the following code:

resources/views/users.blade.php

 

Read also: Laravel Eloquent Join Multiple Table Query Example

 

Hope it can help you to add export to csv and excel button in yajra datatable.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js
ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT