Im begginer at laravel, and I cant figured out how to make a datatable with multiple datatables info
I made a new table called clients
I add a field called clientid to the users table
So a client could have multiple users assigned
The datatable shows succesfully the clients table info, but Im unable to retrive the users related to each client
this is my client model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
use HasFactory;
protected $table="clients";
public $timestamps = false;
public function user()
{
return $this->hasMany(User::class, 'clientid');
}
}
I made this at ClientsDataTable file
public function query(Client $model): QueryBuilder
{
return $model->newQuery()->with(['user']);
}
public function getColumns(): array
{
return [
Column::make('id')->title('ID'),
Column::make('company_name')->title('Client'),
Column::make('user.name')->title('Contacto Principal'),
Column::make('cleintfield3')->title('Field 3'),
Column::make('cleintfield4')->title('Field 4'),
Column::make('cleintfield5')->title('Field 5'),
Column::make('cleintfield6')->title('Field 6'),
Column::make('cleintfield7')->title('Field 7'),
Column::computed('action')
->addClass('text-end text-nowrap')
->exportable(false)
->printable(false)
->width(60)
];
}
Hi Sergio Siqueiros
You need to properly set up your Eloquent relationships and modify your DataTable query to include related data. Please refer to the Yajra datatable documentation here.
https://yajrabox.com/docs/laravel-datatables/11.0/quick-starter
Thank you