Files
generic_table_examples/app/Tables/TableWithRelationships.php

42 lines
1.4 KiB
PHP

<?php
namespace App\Tables;
use App\Models\Product;
use Mmt\GenericTable\Components\Column;
use Mmt\GenericTable\Components\ColumnCollection;
use Mmt\GenericTable\Enums\DatabaseEventQueryState;
use Mmt\GenericTable\Interfaces\IEvent;
use Mmt\GenericTable\Interfaces\IGenericTable;
use Mmt\GenericTable\Support\DatabaseEvent;
use Mmt\GenericTable\Support\GenericTableSettings;
class TableWithRelationships implements IGenericTable, IEvent
{
public GenericTableSettings $tableSettings;
public function __construct()
{
$columns = new ColumnCollection()->add(
new Column('Name'),
new Column('Department'),
new Column('SubDepartment'),
);
$this->tableSettings = new GenericTableSettings(new Product(), $columns, true);
}
public function dispatchCallback(\Mmt\GenericTable\Support\EventArgs $arguments): void
{
if($arguments instanceof DatabaseEvent) {
if($arguments->queryState == DatabaseEventQueryState::INITIALIZING) {
$arguments->builder->join('sub_departments', 'sub_departments.id', 'products.sub_department_id')
->join('departments', 'departments.id', 'sub_departments.department_id')
->select(
'products.name as name',
'departments.name as department',
'sub_departments.name as sub_department'
);
}
}
}
}