You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/tables/docs/09-static-data.md
+361
Original file line number
Diff line number
Diff line change
@@ -417,3 +417,364 @@ public function table(Table $table): Table
417
417
]);
418
418
}
419
419
```
420
+
421
+
## Using an external API as a table data source
422
+
423
+
[Filament's table builder](overview/#introduction) allows you to populate tables with data fetched from any external source—not just [Eloquent models](https://laravel.com/docs/eloquent). This is particularly useful when you want to display data from a REST API or a third-party service.
424
+
425
+
### Fetching data from an external API
426
+
427
+
The example below demonstrates how to consume data from [DummyJSON](https://dummyjson.com), a free fake REST API for placeholder JSON, and display it in a [Filament table](overview/#introduction):
`get('products')` makes a `GET` request to [`https://dummyjson.com/products`](https://dummyjson.com/products). The `collect()` method converts the JSON response into a [Laravel collection](https://laravel.com/docs/collections#main-content). Finally, `get('products', [])` retrieves the array of products from the response. If the key is missing, it safely returns an empty array.
453
+
454
+
<Asidevariant="warning">
455
+
This is a basic example for demonstration purposes only. It's the developer's responsibility to implement proper authentication, authorization, validation, error handling, rate limiting, and other best practices when working with APIs.
456
+
</Aside>
457
+
458
+
<Asidevariant="info">
459
+
DummyJSON returns 30 items by default. You can use the [limit and skip](#external-api-pagination) query parameters to paginate through all items or use [`limit=0`](https://dummyjson.com/docs/products#products-limit_skip) to get all items.
460
+
</Aside>
461
+
462
+
#### Setting the state of a column using API data
463
+
464
+
[Columns](#columns) map to the array keys returned by the `records()` function.
465
+
466
+
When working with the current record inside a column function, set the `$record` type to `array` instead of `Model`. For example, to define a column using the [`state()`](columns/overview#setting-the-state-of-a-column) function, you could do the following:
You can use the [`formatStateUsing()`](columns/text#formatting) method to format the state of a text column without changing the state itself.
484
+
</Aside>
485
+
486
+
### External API sorting
487
+
488
+
You can enable [sorting](columns#sorting) in [columns](columns) even when using an external API as the data source. The example below demonstrates how to pass sorting parameters (`sort_column` and `sort_direction`) to the [DummyJSON](https://dummyjson.com/docs/products#products-sort) API and how they are handled by the API.
`get('products')` makes a `GET` request to [`https://dummyjson.com/products`](https://dummyjson.com/products). The request includes two parameters: `sortBy`, which specifies the column to sort by (e.g., category), and `order`, which specifies the direction of the sort (e.g., asc or desc). The `collect()` method converts the JSON response into a [Laravel collection](https://laravel.com/docs/collections#main-content). Finally, `get('products', [])` retrieves the array of products from the response. If the key is missing, it safely returns an empty array.
519
+
520
+
<Asidevariant="warning">
521
+
This is a basic example for demonstration purposes only. It's the developer's responsibility to implement proper authentication, authorization, validation, error handling, rate limiting, and other best practices when working with APIs.
522
+
</Aside>
523
+
524
+
<Asidevariant="info">
525
+
DummyJSON returns 30 items by default. You can use the [limit and skip](#external-api-pagination) query parameters to paginate through all items or use [`limit=0`](https://dummyjson.com/docs/products#products-limit_skip) to get all items.
526
+
</Aside>
527
+
528
+
### External API searching
529
+
530
+
You can enable [searching](columns#searching) in [columns](columns) even when using an external API as the data source. The example below demonstrates how to pass the `search` parameter to the [DummyJSON](https://dummyjson.com/docs/products#products-search) API and how it is handled by the API.
`get('products/search')` makes a `GET` request to [`https://dummyjson.com/products/search`](https://dummyjson.com/products/search). The request includes the `q` parameter, which is used to filter the results based on the `search` query. The `collect()` method converts the JSON response into a [Laravel collection](https://laravel.com/docs/collections#main-content). Finally, `get('products', [])` retrieves the array of products from the response. If the key is missing, it safely returns an empty array.
561
+
562
+
<Asidevariant="warning">
563
+
This is a basic example for demonstration purposes only. It's the developer's responsibility to implement proper authentication, authorization, validation, error handling, rate limiting, and other best practices when working with APIs.
564
+
</Aside>
565
+
566
+
<Asidevariant="info">
567
+
DummyJSON returns 30 items by default. You can use the [limit and skip](#external-api-pagination) query parameters to paginate through all items or use [`limit=0`](https://dummyjson.com/docs/products#products-limit_skip) to get all items.
568
+
</Aside>
569
+
570
+
### External API filtering
571
+
572
+
You can enable [filtering](filters) in your table even when using an external API as the data source. The example below demonstrates how to pass the `filter` parameter to the [DummyJSON](https://dummyjson.com/docs/products#products-search) API and how it is handled by the API.
If a category filter is selected, the request is made to `/products/category/{category}`; otherwise, it defaults to `/products`. The `get()` method sends a `GET` request to the appropriate endpoint. The `collect()` method converts the JSON response into a [Laravel collection](https://laravel.com/docs/collections#main-content). Finally, `get('products', [])` retrieves the array of products from the response. If the key is missing, it safely returns an empty array.
617
+
618
+
<Asidevariant="warning">
619
+
This is a basic example for demonstration purposes only. It's the developer's responsibility to implement proper authentication, authorization, validation, error handling, rate limiting, and other best practices when working with APIs.
620
+
</Aside>
621
+
622
+
<Asidevariant="info">
623
+
DummyJSON returns 30 items by default. You can use the [limit and skip](#external-api-pagination) query parameters to paginate through all items or use [`limit=0`](https://dummyjson.com/docs/products#products-limit_skip) to get all items.
624
+
</Aside>
625
+
626
+
### External API pagination
627
+
628
+
You can enable [pagination](overview#pagination) when using an external API as the table data source. Filament will pass the current page and the number of records per page to your `records()` function. The example below demonstrates how to construct a `LengthAwarePaginator` manually and fetch paginated data from the [DummyJSON](https://dummyjson.com/docs/products#products-limit_skip) API, which uses `limit` and `skip` parameters for pagination:
629
+
630
+
```php
631
+
public function table(Table $table): Table
632
+
{
633
+
return $table
634
+
->records(function (int $page, int $recordsPerPage): LengthAwarePaginator {
`$page` and `$recordsPerPage` are automatically injected by Filament based on the current pagination state.
661
+
The calculated `skip` value tells the API how many records to skip before returning results for the current page.
662
+
The response contains `products` (the paginated items) and `total` (the total number of available items).
663
+
These values are passed to a `LengthAwarePaginator`, which Filament uses to render pagination controls correctly.
664
+
665
+
<Asidevariant="warning">
666
+
This is a basic example for demonstration purposes only. It's the developer's responsibility to implement proper authentication, authorization, validation, error handling, rate limiting, and other best practices when working with APIs.
667
+
</Aside>
668
+
669
+
### External API full example
670
+
671
+
This example demonstrates how to combine [sorting](#external-api-sorting), [search](#external-api-searching), [category filtering](#external-api-filtering), and [pagination](#external-api-pagination) when using an external API as the data source. The API used here is [DummyJSON](https://dummyjson.com), which supports these features individually but **does not allow combining all of them in a single request**. This is because each feature uses a different endpoint:
672
+
673
+
-[Search](#external-api-searching) is performed through the `/products/search` endpoint using the `q` parameter.
674
+
-[Category filtering](#external-api-filtering) uses the `/products/category/{category}` endpoint.
675
+
-[Sorting](#external-api-sorting) is handled by sending `sortBy` and `order` parameters to the `/products` endpoint.
676
+
677
+
The only feature that can be combined with each of the above is [pagination](#external-api-pagination), since the `limit` and `skip` parameters are supported across all three endpoints.
678
+
679
+
```php
680
+
use Filament\Tables\Columns\ImageColumn;
681
+
use Filament\Tables\Columns\TextColumn;
682
+
use Filament\Tables\Filters\SelectFilter;
683
+
use Filament\Tables\Table;
684
+
use Illuminate\Pagination\LengthAwarePaginator;
685
+
use Illuminate\Support\Collection;
686
+
use Illuminate\Support\Facades\Http;
687
+
use Illuminate\Support\Str;
688
+
689
+
public function table(Table $table): Table
690
+
{
691
+
$baseUrl = 'https://dummyjson.com/';
692
+
693
+
return $table
694
+
->records(function (
695
+
?string $sortColumn,
696
+
?string $sortDirection,
697
+
?string $search,
698
+
array $filters,
699
+
int $page,
700
+
int $recordsPerPage
701
+
) use ($baseUrl): LengthAwarePaginator {
702
+
// Get the selected category from filters (if any)
The [DummyJSON](https://dummyjson.com) API does not support combining sorting, search, and category filtering in a single request.
776
+
</Aside>
777
+
778
+
<Asidevariant="info">
779
+
The [`select`](https://dummyjson.com/docs/products#products-limit_skip) parameter is used to limit the fields returned by the API. This helps reduce payload size and improves performance when rendering the table.
0 commit comments