F
Filament14mo ago
Bloom

Table tabs from database

I looked at the demo application and see the below tab on the table but instead of passing static value i want to retrieve fom the database. : public function getTabs(): array { return [ null => ListRecords\Tab::make('All'), 'new' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'new')), 'processing' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'processing')), 'shipped' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'shipped')), 'delivered' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'delivered')), 'cancelled' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'cancelled')), ]; } instead of new, processing, etc. i want to get a branch name
4 Replies
Kenneth Sese
Kenneth Sese14mo ago
@Bloom getTabs() just returns an array so you can do whatever you there. So you could do something like:
public function getTabs(): array
{
$branches = Branch::all();

$tabs = [];

foreach ($branches as $branch) {
$tabs = [
$branch->name => Tab::make()->query(fn ($query) => $query->where('branch.name', $branch->name)),
];
}

return $tabs;
}
public function getTabs(): array
{
$branches = Branch::all();

$tabs = [];

foreach ($branches as $branch) {
$tabs = [
$branch->name => Tab::make()->query(fn ($query) => $query->where('branch.name', $branch->name)),
];
}

return $tabs;
}
Adjust and refactor as needed of course.
Bloom
BloomOP14mo ago
Thank you it is working, but i want to return all the data also, can you help me with that
Kenneth Sese
Kenneth Sese14mo ago
Lots of ways but one way would be:
// $tabs = [];

$tabs = [
null => Tab::make('All'),
];
// $tabs = [];

$tabs = [
null => Tab::make('All'),
];
So instead of starting with an empty array, just start with your all tab.
Bloom
BloomOP14mo ago
Thank you it worked
Want results from more Discord servers?
Add your server