F
Filament17mo ago
namrata

I want to date to my api from filters

I have called an internal api to show some data in my custom page. That api needs start date and end date parameters. How can I pass the parameters in the api ? The dates are selected in the fliter from datepicker
15 Replies
metts9735
metts973517mo ago
Filament
How to consume an external API with Filament Tables by Leandro C. F...
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
metts9735
metts973517mo ago
After you can write a method to this model https://pastebin.com/XS4EMRXi
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
metts9735
metts973517mo ago
then in getTableQuery(): Builder you can init your model with parameters https://pastebin.com/vCGw509Y I use this approach
Pastebin
protected function getTableQuery(): Builder { $filter...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
metts9735
metts973517mo ago
GitHub
API endpoint filter · Issue #1 · leandrocfe/filament-tables-json-d...
hi, How would you pass parameters (from the filament filters) to the API endpoint URL/body? thanks for your implementation btw :)
namrata
namrataOP17mo ago
Thank you But it looks like Sushi needs model but my internal api is not related to a single model but instead generates a report based on multiple models
LeandroFerreira
LeandroFerreira17mo ago
please share the code..
namrata
namrataOP17mo ago
protected function getTableColumns(): array
{
$report = (new ReportDashboardController)->cakeFlavorProduction(new Request([
// $startDate,
// $endDate,
]));
$res = json_decode($report->getContent(), true)['data'];

return [
TextColumn::make('name')->searchable(),
TextColumn::make('pound')
->label('Pound')->formatStateUsing(function ($record) use ($res) {
return collect($res)->firstWhere('id', $record->id)['pound'] ?? 0;
})->sortable(false)
];
}
protected function getTableColumns(): array
{
$report = (new ReportDashboardController)->cakeFlavorProduction(new Request([
// $startDate,
// $endDate,
]));
$res = json_decode($report->getContent(), true)['data'];

return [
TextColumn::make('name')->searchable(),
TextColumn::make('pound')
->label('Pound')->formatStateUsing(function ($record) use ($res) {
return collect($res)->firstWhere('id', $record->id)['pound'] ?? 0;
})->sortable(false)
];
}
LeandroFerreira
LeandroFerreira17mo ago
Not sure but can you access $this->tableFilters?
namrata
namrataOP17mo ago
$this->tableFilters this gave null but I used HasFilters in the page and then did $this->getTableFilters(), it gave me the whole filter. Since I only need the value, I did $from = $filters[0]->getState()['from'] but error occured. this is the error : Typed property Filament\Tables\Filters\BaseFilter::$table must not be accessed before initialization
LeandroFerreira
LeandroFerreira17mo ago
You can try something like this:
public $apiData = null;

public function updated($name): void
{
if (Str::of($name)->contains('tableFilters.filters')) {
$startDate = data_get($this->tableFilters, 'filters.start_at');
$endDate = data_get($this->tableFilters, 'filters.end_at');

$report = (new ReportDashboardController)->cakeFlavorProduction(new Request([
$startDate,
$endDate,
]));

$this->apiData = json_decode($report->getContent(), true)['data'];
}
}

protected function getTableColumns(): array
{
return [
TextColumn::make('name'),
TextColumn::make('pound')
->getStateUsing(function ($record) {
return $this->apiData ? collect($this->apiData)->firstWhere('id', $record->id)['pound'] ?? 0 : 0;
}),
];
}

protected function getTableFilters(): array
{
return [
Filter::make('filters')
->form([
DatePicker::make('start_at')->default(now()),
DatePicker::make('end_at')->default(now()),
])
];
}
public $apiData = null;

public function updated($name): void
{
if (Str::of($name)->contains('tableFilters.filters')) {
$startDate = data_get($this->tableFilters, 'filters.start_at');
$endDate = data_get($this->tableFilters, 'filters.end_at');

$report = (new ReportDashboardController)->cakeFlavorProduction(new Request([
$startDate,
$endDate,
]));

$this->apiData = json_decode($report->getContent(), true)['data'];
}
}

protected function getTableColumns(): array
{
return [
TextColumn::make('name'),
TextColumn::make('pound')
->getStateUsing(function ($record) {
return $this->apiData ? collect($this->apiData)->firstWhere('id', $record->id)['pound'] ?? 0 : 0;
}),
];
}

protected function getTableFilters(): array
{
return [
Filter::make('filters')
->form([
DatePicker::make('start_at')->default(now()),
DatePicker::make('end_at')->default(now()),
])
];
}
namrata
namrataOP17mo ago
$this->apiData is coming null. The code did not go inside this updated() function.
LeandroFerreira
LeandroFerreira17mo ago
Isn't the updated method called when you change the dates?
namrata
namrataOP17mo ago
No. Even when I select dates the code is not going in the updated method and apiData is returning null
LeandroFerreira
LeandroFerreira17mo ago
Can you share the whole code on github?
namrata
namrataOP17mo ago
Gist
Date filter in api
Date filter in api. GitHub Gist: instantly share code, notes, and snippets.
Want results from more Discord servers?
Add your server