Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

Multiple Datatables in one view / controller Laravel Yajra Service

Hi, I have implemented a datatable from the Laravel example (starter-kit).

The datatable works perfectly - it lists all of my Clients in the clients.index view

public function index(ClientsDataTable $datatable)
{
return $datatable->render('pages.clients.index');
}

and in my ClientsDataTable.php:

public function dataTable(QueryBuilder $query): EloquentDataTable
{
return (new EloquentDataTable($query))
->editColumn('name', function (Client $client) {
return '' . $client->name . '';
})
 // etc
 ->escapeColumns([])
 ->setRowId('id');
}


public function query(Client $model): QueryBuilder
{
 return $model->newQuery();
}


public function html(): HtmlBuilder
{
 return $this->builder()
 ->setTableId('clients-table')
 ->columns($this->getColumns())
 ->minifiedAjax()
 ->dom("<'row'<'col-sm-12 col-md-6'i><'col-sm-12 col-md-6'f>><'row'<'col-sm-12't>><'row'<'col-sm-12 col-md-8'l><'col-sm-12 col-md-4'p>>")
 ->addTableClass('table align-middle table-row-solid gy-5 dataTable no-footer fw-semibold')
 ->setTableHeadClass('text-start fw-bold gs-0 table-light')
 ->responsive(true);
}


public function getColumns(): array
{
 return [
 Column::make('number')
 ->title('Number'),
 // etc
 ];
}


protected function filename(): string
{
 return 'Clients_' . date('YmdHis');
}

but now I am building the clients.show view where I would like to put two seperate datatables related to a specific Client that I'm viewing:

  1. Client Contacts - a list of contacts related to the client_id in the database
  2. Client Orders - a list of orders related to the client_id in the database

I have two other models ClientContact ClientOrder

How should I properly code the controller / datatable class / the view for the tables and all requests and functionality to work properly?

Thanks in advance :) Mati

Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (1)

To implement multiple DataTables in one controller, you can try to follow these steps: Note: we did not test it. Please refer to the Yajra Datatables documentation for more details.

  1. Controller: In your ClientsController, you can modify the show method to pass data for both Client Contacts and Client Orders:
use App\DataTables\ClientContactsDataTable;
use App\DataTables\ClientOrdersDataTable;
use App\Models\Client;

public function show(Client $client, ClientContactsDataTable $contactsDataTable, ClientOrdersDataTable $ordersDataTable)
{
    return view('pages.clients.show', [
        'client' => $client,
        'contactsDataTable' => $contactsDataTable->html()->ajax([
            'url' => route('clients.contacts', $client->id),
            'type' => 'GET',
        ]),
        'ordersDataTable' => $ordersDataTable->html()->ajax([
            'url' => route('clients.orders', $client->id),
            'type' => 'GET',
        ]),
    ]);
}
  1. DataTables for Contacts and Orders: Create separate DataTable classes for ClientContactsDataTable and ClientOrdersDataTable similarly to what you did for the ClientsDataTable.

  2. Views: In your clients.show view, you can include the DataTables:

@extends('layouts.app')

@section('content')
    

{{ $client->name }}

Client Contacts

{!! $contactsDataTable->table(['class' => 'table table-bordered table-hover']) !!}

Client Orders

{!! $ordersDataTable->table(['class' => 'table table-bordered table-hover']) !!}
@endsection @push('scripts') {!! $contactsDataTable->scripts() !!} {!! $ordersDataTable->scripts() !!} @endpush
  1. Routes: Define routes for fetching data for both Contacts and Orders:
// web.php
Route::get('/clients/{client}/contacts', 'ClientContactsController@index')->name('clients.contacts');
Route::get('/clients/{client}/orders', 'ClientOrdersController@index')->name('clients.orders');
  1. Controller for Contacts and Orders: Create controllers (ClientContactsController and ClientOrdersController) with methods to handle DataTables requests for Contacts and Orders.

Now, when you navigate to the clients.show page, the DataTables for Client Contacts and Client Orders should be populated with data related to the specific client.

Note: Adjust the namespaces and class names according to your application structure. This is a basic outline, and you may need to make adjustments based on your specific requirements.

Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(