Table Columns from Database

Been trying to figure this for weeks, and gotten to the point I just need help! I'm wanting a custom table, which gets the rows from a database table as normal, and the columns from another database table (rather than the standard database column names) to create a matrix of sorts. Excusing the below mess showing where I'm up to (just trying to get it working at this stage), the action doesn't seem to be running when I click on a cell.
19 Replies
Hightower
Hightower4mo ago
public static function build(Table $table): Table
{
$academies = Academy::all();

$columns = [
TextColumn::make('title')
->searchable()
->sortable(),
];

// Loop through each tenant and create a column
foreach ($academies as $academy) {
$columns[] = TextColumn::make('academy_' . $academy->id)
->label(static::abbreviate($academy->name))
->alignCenter()
->getStateUsing(function ($record) use ($academy) {
$data = Document::where('academy_id', $academy->id)
->where('matrix_id', $record->id)
?->first();

if ($data) {
return date_format(Document::find($data->id)
->renewal_date, 'd/m/Y');
}
})
->placeholder('+')
->action(function (DocumentMatrix $record) use ($academy) {

Action::make('select')
->label($academy->name)
->action(function () use ($record, $academy): void {
dd('Row ID: '.$record->id, 'Column ID: '.$academy->id);
})
->slideOver();
});
}

return $table
->columns($columns)
->defaultSort('title')
->filters([])
->actions([
EditAction::make(),
])
->bulkActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}
public static function build(Table $table): Table
{
$academies = Academy::all();

$columns = [
TextColumn::make('title')
->searchable()
->sortable(),
];

// Loop through each tenant and create a column
foreach ($academies as $academy) {
$columns[] = TextColumn::make('academy_' . $academy->id)
->label(static::abbreviate($academy->name))
->alignCenter()
->getStateUsing(function ($record) use ($academy) {
$data = Document::where('academy_id', $academy->id)
->where('matrix_id', $record->id)
?->first();

if ($data) {
return date_format(Document::find($data->id)
->renewal_date, 'd/m/Y');
}
})
->placeholder('+')
->action(function (DocumentMatrix $record) use ($academy) {

Action::make('select')
->label($academy->name)
->action(function () use ($record, $academy): void {
dd('Row ID: '.$record->id, 'Column ID: '.$academy->id);
})
->slideOver();
});
}

return $table
->columns($columns)
->defaultSort('title')
->filters([])
->actions([
EditAction::make(),
])
->bulkActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}
Any help would be hugely appreciated
toeknee
toeknee4mo ago
Why are they not just releationships?
Dennis Koch
Dennis Koch4mo ago
What does your dd('Row ID: '.$record->id, 'Column ID: '.$academy->id); output? I guess you might just need a ->record($academy) on either your column or action (or both)
Hightower
Hightower4mo ago
Nothing happens on the action, i dont get the output of dd() It’s like a blank click I imagine they will be when I get further down the line, but at the moment I'm not even that far. I just need the table working so I can grab the column ID as well as the row ID when a particular cell is clicked
awcodes
awcodes4mo ago
Wonder if it’s because you’re not returning the Action::make(‘select’)
Hightower
Hightower4mo ago
No, sorry, noticed that as soon as I posted, put a return on there and still nothing. It’s like there’s no action on click, even a blank dd() doesn’t output anything
awcodes
awcodes4mo ago
Do you have spatie’s ray? Sometimes dd doesn’t work in callbacks in livewire.
Hightower
Hightower4mo ago
Ah no, I’m just a bedroom coder so haven’t stumped up for that But instead of a dd(), if I try some real action it may work
awcodes
awcodes4mo ago
Maybe try a Log instead
Hightower
Hightower4mo ago
Will do, will update thread tomorrow Nothing hitting the log either It's almost like the action isn't running Wasn't 100% (or 10%) confident the code structure is right, does it look like it should work?
Dennis Koch
Dennis Koch4mo ago
Another thing that comes to my mind: You create multiple actions with the same name ("select"). That will also lead to errors. Probably not the current error though
Hightower
Hightower4mo ago
Thanks, I've changed to:
return Action::make('select_' . $record->id . '_' . $academy->id)
return Action::make('select_' . $record->id . '_' . $academy->id)
Still no luck with the action running though Any further ideas anyone?
toeknee
toeknee3mo ago
When are you calling the build function? Post page load?
Hightower
Hightower3mo ago
It's called in the DocumentMatrixResource, I just always split it out to try and keep the resource clean:
public static function table(Table $table): Table
{
return DocumentMatrixTable::build($table);
}
public static function table(Table $table): Table
{
return DocumentMatrixTable::build($table);
}
toeknee
toeknee3mo ago
I assume this is a custom livewire component? Are you mounting it?
Hightower
Hightower3mo ago
Hi @toeknee - apologies, been on holiday so only just seen this. I'm not sure what you mean by custom livewire component and mounting it. I've created a new resource, and I am trying to adapt the table within the resource to fit my needs.
// Loop through each tenant and create a column
foreach ($academies as $academy) {
Log::debug('Academy: ' . $academy->id);
$columns[] = TextColumn::make('academy_' . $academy->id)
->label(static::abbreviate($academy->name))
->alignCenter()
//->extraAttributes(['style' => 'background-color: red;'])
->getStateUsing(function ($record) use ($academy) {
$data = Document::where('academy_id', $academy->id)
->where('matrix_id', $record->id)
?->first();

if ($data) {
return date_format(Document::find($data->id)
->renewal_date, 'd/m/Y');
}
})
->record($academy)
->placeholder('+')
->action(
Action::make('select')
->label($academy->name)
->action(function (DocumentMatrix $resource, array $data) use ($academy): void {
$document = Document::find($data['document']);
Log::debug('Tried to add ' . $document->title . ' to ' . $resource->title . ' for ' . $academy->name);
})
->record($academy)
->slideOver()
->form(
function (DocumentMatrix $resource) use ($academy) {
return [
// Define the form fields that will appear in the modal
Select::make('document')
->label('Select a document to assign to ' . $resource->title . ' for ' . $academy->name)
->options(
Document::where('academy_id', $academy->id)
->pluck('title', 'id')
)
->required()
->searchable(),
];
}
)
);
}
// Loop through each tenant and create a column
foreach ($academies as $academy) {
Log::debug('Academy: ' . $academy->id);
$columns[] = TextColumn::make('academy_' . $academy->id)
->label(static::abbreviate($academy->name))
->alignCenter()
//->extraAttributes(['style' => 'background-color: red;'])
->getStateUsing(function ($record) use ($academy) {
$data = Document::where('academy_id', $academy->id)
->where('matrix_id', $record->id)
?->first();

if ($data) {
return date_format(Document::find($data->id)
->renewal_date, 'd/m/Y');
}
})
->record($academy)
->placeholder('+')
->action(
Action::make('select')
->label($academy->name)
->action(function (DocumentMatrix $resource, array $data) use ($academy): void {
$document = Document::find($data['document']);
Log::debug('Tried to add ' . $document->title . ' to ' . $resource->title . ' for ' . $academy->name);
})
->record($academy)
->slideOver()
->form(
function (DocumentMatrix $resource) use ($academy) {
return [
// Define the form fields that will appear in the modal
Select::make('document')
->label('Select a document to assign to ' . $resource->title . ' for ' . $academy->name)
->options(
Document::where('academy_id', $academy->id)
->pluck('title', 'id')
)
->required()
->searchable(),
];
}
)
);
}
So I've got the action working, whereby the form loads and then I can select a drop down and click submit and the action logs. However, it always tries to add it for the last academy (ID: 8). Any ideas why? I'm rocked backwards and forwards for a while on this with no joy. Actually thought I figured it yesterday, could have sworn it was working as intended, but then it stopped. I went through and undid all my changes to try and get it back to the point but never got it working again so it was either a fluke or I dreamt it.
->action(
function (DocumentMatrix $record) use ($academy) {
return Action::make('select')
->label($academy->name)
->action(function (DocumentMatrix $resource, array $data) use ($academy): void {
$document = Document::find($data['document']);
Log::debug('Tried to add ' . $document->title . ' to ' . $resource->title . ' for ' . $academy->name);
})
->slideOver()
->form(
function (DocumentMatrix $resource) use ($academy) {
Log::debug($academy);
return [
// Define the form fields that will appear in the modal
Select::make('document')
->label('Select a document to assign to ' . $resource->title . ' for ' . $academy->name)
->options(
Document::where('academy_id', $academy->id)
->pluck('title', 'id')
)
->required()
->searchable(),
];
}
);
}
);
->action(
function (DocumentMatrix $record) use ($academy) {
return Action::make('select')
->label($academy->name)
->action(function (DocumentMatrix $resource, array $data) use ($academy): void {
$document = Document::find($data['document']);
Log::debug('Tried to add ' . $document->title . ' to ' . $resource->title . ' for ' . $academy->name);
})
->slideOver()
->form(
function (DocumentMatrix $resource) use ($academy) {
Log::debug($academy);
return [
// Define the form fields that will appear in the modal
Select::make('document')
->label('Select a document to assign to ' . $resource->title . ' for ' . $academy->name)
->options(
Document::where('academy_id', $academy->id)
->pluck('title', 'id')
)
->required()
->searchable(),
];
}
);
}
);
If I change the action to above so that it is a function, running a dd($academy) returns the right academy whatever is clicked on - but the Action doesn't work in this format So, I can have the action work through
->action(Action::make.....
->action(Action::make.....
But that always uses the last academy. Or I can have it work with the right academy using
->action(function($record) use ($academy)....
->action(function($record) use ($academy)....
But the Action doesn't work then (i.e. the form etc won't load on click) Seems this is the 'issue' I'm coming across:
Hightower
Hightower3mo ago
GitHub
The column action doesn't work if a closure is used as parameter · ...
Package filament/filament Package Version v2.17.40 Laravel Version v10.11.0 Livewire Version v2.12.3 PHP Version PHP 8.2.4 Problem description The modal is not displayed if a closure is used as a p...
Hightower
Hightower3mo ago
Would appreciate any advice on how best to work this
toeknee
toeknee3mo ago
->action(
Action::make('select')
->label($academy->name)
->action(function (DocumentMatrix $resource, array $data) use ($academy): void {
$document = Document::find($data['document']);
Log::debug('Tried to add ' . $document->title . ' to ' . $resource->title . ' for ' . $academy->name);
})
->slideOver()
->form(
function (DocumentMatrix $resource) use ($academy) {
Log::debug($academy);
return [
// Define the form fields that will appear in the modal
Select::make('document')
->label('Select a document to assign to ' . $resource->title . ' for ' . $academy->name)
->options(
Document::where('academy_id', $academy->id)
->pluck('title', 'id')
)
->required()
->searchable(),
];
}
);

);
->action(
Action::make('select')
->label($academy->name)
->action(function (DocumentMatrix $resource, array $data) use ($academy): void {
$document = Document::find($data['document']);
Log::debug('Tried to add ' . $document->title . ' to ' . $resource->title . ' for ' . $academy->name);
})
->slideOver()
->form(
function (DocumentMatrix $resource) use ($academy) {
Log::debug($academy);
return [
// Define the form fields that will appear in the modal
Select::make('document')
->label('Select a document to assign to ' . $resource->title . ' for ' . $academy->name)
->options(
Document::where('academy_id', $academy->id)
->pluck('title', 'id')
)
->required()
->searchable(),
];
}
);

);
Want results from more Discord servers?
Add your server