F
Filament7mo ago
Jonas

Using table builder with an external API

Hi. I have been following the guide here: https://filamentphp.com/community/how-to-consume-an-external-api-with-filament-tables This works good an all, however here is my problem: - I need to define query paramaters on the actual API, not the model (so in order to first populate the model, i need to define variables when polling the API). How would i approach something like this?
Filament
How to consume an external API with Filament Tables by Leandro Ferr...
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
1 Reply
Jonas
Jonas7mo ago
class MissingVoucherAPI extends Model
{
use \Sushi\Sushi;

protected int $teamID;
protected int $accountID;

protected array $schema = [
'id' => 'integer',
'date' => 'date',
'voucher_id' => 'integer',
'voucher_number' => 'string',
'description' => 'string',
'supplier' => 'string',
'customer_id' => 'integer',
];

protected function sushiShouldCache()
{
return false;
}

public function getRows(): array
{

$products = Http::get("https://dummyjson.com/products?team={$this->teamID}&{$this->accountID}")->json();


return [];
}

}
class MissingVoucherAPI extends Model
{
use \Sushi\Sushi;

protected int $teamID;
protected int $accountID;

protected array $schema = [
'id' => 'integer',
'date' => 'date',
'voucher_id' => 'integer',
'voucher_number' => 'string',
'description' => 'string',
'supplier' => 'string',
'customer_id' => 'integer',
];

protected function sushiShouldCache()
{
return false;
}

public function getRows(): array
{

$products = Http::get("https://dummyjson.com/products?team={$this->teamID}&{$this->accountID}")->json();


return [];
}

}
And where i will be calling it:
public function table(Table $table): Table
{


return $table
->query(MissingVoucherAPI::query() // ** HERE I ALSO NEED TO DEFINE teamID and accountID)
->emptyStateHeading('Ingen data')
->columns([
Tables\Columns\TextColumn::make('type')
->default('Mangler kvittering/kommentar'),
Tables\Columns\TextColumn::make('date'),
Tables\Columns\TextColumn::make('voucher_id'),
Tables\Columns\TextInputColumn::make('description'),
Tables\Columns\TextColumn::make('supplier'),
])
->bulkActions([
Tables\Actions\BulkAction::make('send_to_customer')
->requiresConfirmation()
->action(fn (Collection $records) => $records->each->sendToCustomer()),
]);
}
public function table(Table $table): Table
{


return $table
->query(MissingVoucherAPI::query() // ** HERE I ALSO NEED TO DEFINE teamID and accountID)
->emptyStateHeading('Ingen data')
->columns([
Tables\Columns\TextColumn::make('type')
->default('Mangler kvittering/kommentar'),
Tables\Columns\TextColumn::make('date'),
Tables\Columns\TextColumn::make('voucher_id'),
Tables\Columns\TextInputColumn::make('description'),
Tables\Columns\TextColumn::make('supplier'),
])
->bulkActions([
Tables\Actions\BulkAction::make('send_to_customer')
->requiresConfirmation()
->action(fn (Collection $records) => $records->each->sendToCustomer()),
]);
}
Want results from more Discord servers?
Add your server