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

DAtatables relationships metronic 8 2 6 laravel


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)
];
}


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)


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.



Thank you


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  :(