Introducing CrudHunt:Open-source Full-stack CRUDs for Next.js by KeenThemes
Browse CrudHunt

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
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • 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.

https://yajrabox.com/docs/laravel-datatables/11.0/quick-starter

Thank you


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(