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:
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
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.
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',
]),
]);
}
DataTables for Contacts and Orders: Create separate DataTable classes for ClientContactsDataTable and ClientOrdersDataTable similarly to what you did for the ClientsDataTable.
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
// web.php
Route::get('/clients/{client}/contacts', 'ClientContactsController@index')->name('clients.contacts');
Route::get('/clients/{client}/orders', 'ClientOrdersController@index')->name('clients.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.