Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Combining Yajra Datatables with other Laravel packages


Combining Yajra Datatables with other Laravel packages


To combine Yajra Datatables with other Laravel packages, you can follow these steps:

1. Install the required packages using Composer. For example, to use Yajra Datatables with Laravel Eloquent, you would install the following packages:

bash
composer require yajra/laravel-datatables-oracle:"~9.0"
composer require yajra/laravel-datatables-fractal:"~3.0"

2. Configure the packages by publishing their configuration files and modifying them as needed. For example, to configure Yajra Datatables, you can run:

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

3. Create a DataTable class for each table you want to display. For example, to create a DataTable for the `users` table, you can run:

bash
php artisan datatables:make UsersDataTable

4. Customize the DataTable class by defining the columns, actions, and other options. For example, to add a column for the user's name and email, you can modify the `columns()` method:

php
public function columns()
{
    return [
        Column::make('name'),
        Column::make('email'),
    ];
}

5. In your controller, pass the DataTable instance to the view. For example:

php
public function index(UsersDataTable $dataTable)
{
    return $dataTable->render('users.index');
}

6. In your view, render the DataTable using the `@extends` and `@section` directives. For example:

html
@extends('layouts.app')

@section('content')
    {!! $dataTable->table() !!}
@endsection

@push('scripts')
    {!! $dataTable->scripts() !!}
@endpush

7. If you need to use other packages with Yajra Datatables, such as Laravel Fractal for data transformation, you can configure them in the DataTable class. For example, to use Fractal, you can modify the `with()` method:

php
public function with($data)
{
    return fractal()
        ->collection($data)
        ->transformWith(new UserTransformer())
        ->toArray();
}

By following these steps and customizing the configuration as needed, you can successfully combine Yajra Datatables with other Laravel packages to create powerful data-driven applications[1][4].

Citations:
[1] https://stackoverflow.com/questions/45633297/laravel-datatables-multiple-tables-on-one-view
[2] https://github.com/yajra/laravel-datatables/discussions/2599
[3] https://laracasts.com/discuss/channels/laravel/yajra-datatable-with-relationship
[4] https://www.medianova.com/maximizing-web-performance-with-yajra-datatables/
[5] https://github.com/yajra/laravel-datatables/issues/2143