F
Filament9mo ago
nowak

How to fetch current table records in table query

I am building a dynamic table, where I would like to use a list of the current records in the table I have queried. Here is a code snippet of the table I am building:
public static function table(Table $table): Table
{
return $table
->query(GroupOrderResource::getEloquentQuery()
->whereDate('deadline', '=', now()->toDateString()))
->defaultSort('sort')
->reorderable('sort')
->columns([
TextInputColumn::make('route')
->afterStateUpdated(function ($record) {
OrderService::handleRouteChange($record);
}),
]);
}
public static function table(Table $table): Table
{
return $table
->query(GroupOrderResource::getEloquentQuery()
->whereDate('deadline', '=', now()->toDateString()))
->defaultSort('sort')
->reorderable('sort')
->columns([
TextInputColumn::make('route')
->afterStateUpdated(function ($record) {
OrderService::handleRouteChange($record);
}),
]);
}
In my ->afterStateUpdated I would like to fetch and use the the query of all records in the table, without needing to run the query again:
GroupOrderResource::getEloquentQuery()->whereDate('deadline', '=', now()->toDateString())
GroupOrderResource::getEloquentQuery()->whereDate('deadline', '=', now()->toDateString())
Because my query could change based on filters in the future, so the easiest would be to fetch the records from the table dirctly.
1 Reply
nowak
nowakOP9mo ago
I found a workaround for now. Since the table queries will only list the same record once, I can deduct the queried records from the details of the updated record, like so:
TextInputColumn::make('route')
->afterStateUpdated(function ($record) {
OrderService::handleRouteChange($record);
}),
TextInputColumn::make('route')
->afterStateUpdated(function ($record) {
OrderService::handleRouteChange($record);
}),
And then in OrderService::handleRouteChange():
public static function handleRouteChange(GroupOrder $groupOrder): void
{
$deadline = $groupOrder->deadline;
$groupOrders = GroupOrder::where('deadline', $deadline)->get();
}
public static function handleRouteChange(GroupOrder $groupOrder): void
{
$deadline = $groupOrder->deadline;
$groupOrders = GroupOrder::where('deadline', $deadline)->get();
}
But I would still love to know it it is possible to get the query directly from the filament table in afterStateUpdated.
Want results from more Discord servers?
Add your server