From 21479c6fae9302277184fcbbea682c5dc66e8613 Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 14 Apr 2025 11:17:05 -0300 Subject: [PATCH 01/19] update docs using external APIs as table data sources --- packages/tables/docs/09-static-data.md | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index d6d7ae7a84..e5d60662ab 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -417,3 +417,77 @@ public function table(Table $table): Table ]); } ``` + +## Using an External API as a Table Data Source + +[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. + +### Fetching Data from a Laravel API + +Below is a demonstrative example of how to consume data from a [Laravel-based API](https://laravel.com/docs/installation#laravel-the-api-backend) and display it in a [Filament table](overview/#introduction): + +```php +use Filament\Tables\Columns\IconColumn; +use Filament\Tables\Columns\TextColumn; +use Filament\Tables\Table; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Http; + +public function table(Table $table): Table +{ + return $table + ->records(fn (): array => Http::baseUrl('https://laravel-api.test/api') + ->get('posts') + ->collect() + ->get('data') + ) + ->columns([ + TextColumn::make('title'), + TextColumn::make('slug'), + IconColumn::make('is_featured') + ->boolean(), + ]); +} +``` + + + +### Example Laravel API Endpoint + +Here's an example of how you could define a [Laravel API route](https://laravel.com/docs/routing#api-routes) that returns a list of posts using a [Resource Collection](https://laravel.com/docs/eloquent-resources#resource-collections): + +```php +use App\Models\Post; +use Illuminate\Support\Facades\Route; + +Route::get('/posts', function () { + return Post::all()->toResourceCollection(); +}); +``` + +The [PostResource](https://laravel.com/docs/eloquent-resources#introduction) transforms your Post Model into a clean JSON structure: + +```php +use Illuminate\Http\Resources\Json\JsonResource; + +class PostResource extends JsonResource +{ + public function toArray($request): array + { + return [ + 'id' => $this->id, + 'title' => $this->title, + 'slug' => $this->slug, + 'is_featured' => $this->is_featured, + ]; + } +} +``` + +This is a simplified example intended to demonstrate the use of [Laravel Resource Collections](https://laravel.com/docs/eloquent-resources#resource-collections). **It is not ready for production**. + + \ No newline at end of file From e0fe9b27c20139a6c60b8448f723742b32133ad3 Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 14 Apr 2025 15:56:36 -0300 Subject: [PATCH 02/19] Enhances API data handling and adds sorting example. --- packages/tables/docs/09-static-data.md | 58 +++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index e5d60662ab..d0a99ccf41 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -430,7 +430,6 @@ Below is a demonstrative example of how to consume data from a [Laravel-based AP use Filament\Tables\Columns\IconColumn; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; -use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; public function table(Table $table): Table @@ -439,7 +438,7 @@ public function table(Table $table): Table ->records(fn (): array => Http::baseUrl('https://laravel-api.test/api') ->get('posts') ->collect() - ->get('data') + ->get('data', []) ) ->columns([ TextColumn::make('title'), @@ -488,6 +487,61 @@ class PostResource extends JsonResource This is a simplified example intended to demonstrate the use of [Laravel Resource Collections](https://laravel.com/docs/eloquent-resources#resource-collections). **It is not ready for production**. + + +### Sorting with an External API + +You can enable [sorting](columns#sorting) in [columns](columns) even when the data source is an external API. Below is an example of how to pass sorting parameters (`sort_column` and `sort_direction`) to a [Laravel API](https://laravel.com/docs/installation#laravel-the-api-backend) and handle them on the backend. + +```php +use Filament\Tables\Columns\TextColumn; +use Filament\Tables\Table; +use Illuminate\Support\Facades\Http; + +public function table(Table $table): Table +{ + return $table + ->records(function (?string $sortColumn, ?string $sortDirection): array { + $response = Http::baseUrl('https://laravel-api.test/api') + ->get('posts', [ + 'sort_column' => $sortColumn, + 'sort_direction' => $sortDirection, + ]); + + return $response + ->collect() + ->get('data', []); + }) + ->columns([ + TextColumn::make('title') + ->sortable(), + ]); +} +``` + +On the backend, your Laravel API needs to receive and process the `sort_column` and `sort_direction` parameters sent by the frontend. In the example below, the `/posts` endpoint dynamically applies ordering to the query based on those parameters: + +```php +use App\Models\Post; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Route; + +Route::get('/posts', function (Request $request) { + $query = Post::query(); + + if ($sortColumn = $request->query('sort_column')) { + $sortDirection = $request->query('sort_direction', 'asc'); + $query->orderBy($sortColumn, $sortDirection); + } + + return $query + ->get() + ->toResourceCollection(); +}); +``` + \ No newline at end of file From 58792b64a767fcfd5f85695a50d5b0a422123b5c Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 14 Apr 2025 16:23:10 -0300 Subject: [PATCH 03/19] Introduces an example demonstrating how to enable search functionality in tables using an external API. --- packages/tables/docs/09-static-data.md | 60 +++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index d0a99ccf41..6392c74410 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -544,4 +544,62 @@ Route::get('/posts', function (Request $request) { \ No newline at end of file + + +### Searching with an External API + +You can enable [searching](columns#searching) in [columns](columns) even when the data source is an external API. Below is an example of how to pass a `search` parameter to a [Laravel API](https://laravel.com/docs/installation#laravel-the-api-backend) and filter the results on the backend. + +```php +use Filament\Tables\Columns\TextColumn; +use Filament\Tables\Table; +use Illuminate\Support\Facades\Http; + +public function table(Table $table): Table +{ + return $table + ->records(function (?string $search): array { + $response = Http::baseUrl('https://laravel-api.test/api') + ->get('posts', [ + 'search' => $search, + ]); + + return $response + ->collect() + ->get('data', []); + }) + ->columns([ + TextColumn::make('title') + ->searchable(), + ]); +} +``` + +On the backend, your Laravel API needs to receive and apply the `search` parameter to the query. In the example below, the `/posts` endpoint dynamically filters the records by title: + +```php +use App\Models\Post; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Route; + +Route::get('/posts', function (Request $request) { + $query = Post::query(); + + if ($search = $request->query('search')) { + $query->where('title', 'like', "%{$search}%"); + } + + return $query + ->get() + ->toResourceCollection(); +}); +``` + + + + From 03ce7919899bd6c847f2eefa1e6f117cb4ebc8d7 Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 14 Apr 2025 17:04:59 -0300 Subject: [PATCH 04/19] Updates documentation to include filtering examples for external APIs. --- packages/tables/docs/09-static-data.md | 69 +++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index 6392c74410..78f80614f2 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -491,7 +491,7 @@ This is a simplified example intended to demonstrate the use of [Laravel Resourc 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. -### Sorting with an External API +### External API Sorting You can enable [sorting](columns#sorting) in [columns](columns) even when the data source is an external API. Below is an example of how to pass sorting parameters (`sort_column` and `sort_direction`) to a [Laravel API](https://laravel.com/docs/installation#laravel-the-api-backend) and handle them on the backend. @@ -546,7 +546,7 @@ Route::get('/posts', function (Request $request) { 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. -### Searching with an External API +### External API Searching You can enable [searching](columns#searching) in [columns](columns) even when the data source is an external API. Below is an example of how to pass a `search` parameter to a [Laravel API](https://laravel.com/docs/installation#laravel-the-api-backend) and filter the results on the backend. @@ -603,3 +603,68 @@ Route::get('/posts', function (Request $request) { This example uses the [Example Laravel API Endpoint](#example-laravel-api-endpoint) along with a corresponding [API Resource](https://laravel.com/docs/eloquent-resources#introduction) to return structured data from the API. The response includes a `data` key that contains the formatted items displayed in the Filament table. + +### External API Filtering + +You can enable [filtering](filters) in your table even when the data source is an external API. Below is an example of how to pass a `filter` parameter (`is_featured`) to a [Laravel API](https://laravel.com/docs/installation#laravel-the-api-backend) and filter the results on the backend. + +```php +use Filament\Tables\Columns\IconColumn; +use Filament\Tables\Columns\TextColumn; +use Filament\Tables\Filters\Filter; +use Filament\Tables\Table; +use Illuminate\Support\Facades\Http; + +public function table(Table $table): Table +{ + return $table + ->records(function (array $filters): array { + $response = Http::baseUrl('https://laravel-api.test/api') + ->get('posts', [ + 'is_featured' => $filters['is_featured']['isActive'] ?? false, + ]); + + return $response + ->collect() + ->get('data', []); + }) + ->columns([ + TextColumn::make('title'), + TextColumn::make('slug'), + IconColumn::make('is_featured') + ->boolean(), + ]) + ->filters([ + Filter::make('is_featured'), + ]); +} +``` + +On the backend, your Laravel API needs to receive and apply the `is_featured` parameter to the query. In the example below, the `/posts` endpoint dynamically filters the records to return only featured items: + +```php +use App\Models\Post; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Route; + +Route::get('/posts', function (Request $request) { + $query = Post::query(); + + if ($request->boolean('is_featured')) { + $query->where('is_featured', true); + } + + return $query + ->get() + ->toResourceCollection(); +}); +``` + + + + \ No newline at end of file From 35ad0f345aab43e5d51c9a7800da1fe1e116171b Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 14 Apr 2025 17:05:42 -0300 Subject: [PATCH 05/19] Adds info aside explaining API resource usage --- packages/tables/docs/09-static-data.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index 78f80614f2..dae5c84f24 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -546,6 +546,11 @@ Route::get('/posts', function (Request $request) { 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. + + ### External API Searching You can enable [searching](columns#searching) in [columns](columns) even when the data source is an external API. Below is an example of how to pass a `search` parameter to a [Laravel API](https://laravel.com/docs/installation#laravel-the-api-backend) and filter the results on the backend. From cc7a6b28901873c636de240d75b27f43ead19bba Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 14 Apr 2025 18:01:59 -0300 Subject: [PATCH 06/19] Updates documentation to include pagination examples for external APIs. --- packages/tables/docs/09-static-data.md | 60 +++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index dae5c84f24..9997ec0a1c 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -672,4 +672,62 @@ Route::get('/posts', function (Request $request) { \ No newline at end of file + + +### External API Pagination + +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. In this example, you can use these parameters to your [Laravel API](https://laravel.com/docs/installation#laravel-the-api-backend) and construct a `LengthAwarePaginator` manually. + +```php +use Filament\Tables\Columns\TextColumn; +use Filament\Tables\Table; +use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Facades\Http; + +public function table(Table $table): Table +{ + return $table + ->records(function (int $page, int $recordsPerPage): LengthAwarePaginator { + $response = Http::baseUrl('https://laravel-api.test/api') + ->get('posts', [ + 'page' => $page, + 'per_page' => $recordsPerPage, + ]) + ->collect(); + + return new LengthAwarePaginator( + items: $response['data'], + total: $response['meta']['total'], + perPage: $response['meta']['per_page'], + currentPage: $response['meta']['current_page'] + ); + }) + ->columns([ + TextColumn::make('title'), + ]); +} +``` + +On the backend, your Laravel API should receive the `page` and `per_page` parameters, apply pagination using [paginate()](https://laravel.com/docs/eloquent-resources#pagination) method, and return a [Resource Collection](https://laravel.com/docs/eloquent-resources#resource-collections) that includes the pagination metadata: + +```php +use App\Models\Post; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Route; + +Route::get('/posts', function (Request $request) { + $perPage = $request->query('per_page', 10); + + return Post::paginate($perPage) + ->toResourceCollection(); +}); +``` + + + + From a5bbf17168f77561ae3fc87a42ad32d36daf4341 Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 14 Apr 2025 21:57:41 -0300 Subject: [PATCH 07/19] Update API examples to use DummyJSON for demonstration --- packages/tables/docs/09-static-data.md | 95 ++++++-------------------- 1 file changed, 21 insertions(+), 74 deletions(-) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index 9997ec0a1c..2315d0cab7 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -422,9 +422,9 @@ public function table(Table $table): Table [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. -### Fetching Data from a Laravel API +### Fetching data from an external API -Below is a demonstrative example of how to consume data from a [Laravel-based API](https://laravel.com/docs/installation#laravel-the-api-backend) and display it in a [Filament table](overview/#introduction): +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): ```php use Filament\Tables\Columns\IconColumn; @@ -435,16 +435,16 @@ use Illuminate\Support\Facades\Http; public function table(Table $table): Table { return $table - ->records(fn (): array => Http::baseUrl('https://laravel-api.test/api') - ->get('posts') + ->records(fn (): array => Http::baseUrl('https://dummyjson.com') + ->get('products') ->collect() - ->get('data', []) + ->get('products', []) ) ->columns([ TextColumn::make('title'), - TextColumn::make('slug'), - IconColumn::make('is_featured') - ->boolean(), + TextColumn::make('category'), + TextColumn::make('price') + ->money(), ]); } ``` @@ -453,47 +453,13 @@ public function table(Table $table): Table 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. -### Example Laravel API Endpoint - -Here's an example of how you could define a [Laravel API route](https://laravel.com/docs/routing#api-routes) that returns a list of posts using a [Resource Collection](https://laravel.com/docs/eloquent-resources#resource-collections): - -```php -use App\Models\Post; -use Illuminate\Support\Facades\Route; - -Route::get('/posts', function () { - return Post::all()->toResourceCollection(); -}); -``` - -The [PostResource](https://laravel.com/docs/eloquent-resources#introduction) transforms your Post Model into a clean JSON structure: - -```php -use Illuminate\Http\Resources\Json\JsonResource; - -class PostResource extends JsonResource -{ - public function toArray($request): array - { - return [ - 'id' => $this->id, - 'title' => $this->title, - 'slug' => $this->slug, - 'is_featured' => $this->is_featured, - ]; - } -} -``` - -This is a simplified example intended to demonstrate the use of [Laravel Resource Collections](https://laravel.com/docs/eloquent-resources#resource-collections). **It is not ready for production**. - - +### Setting the state of a column + +[Columns](#columns) map to the array keys returned by the `records()` function. + +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: + +```php +use Filament\Tables\Columns\TextColumn; +use Illuminate\Support\Str; + +TextColumn::make('category') + ->state(fn (array $record): string => Str::headline($record['category'])), +``` + + + ### External API Sorting 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. @@ -594,58 +612,43 @@ public function table(Table $table): Table ### External API Pagination -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. In this example, you can use these parameters to your [Laravel API](https://laravel.com/docs/installation#laravel-the-api-backend) and construct a `LengthAwarePaginator` manually. +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: ```php -use Filament\Tables\Columns\TextColumn; -use Filament\Tables\Table; -use Illuminate\Pagination\LengthAwarePaginator; -use Illuminate\Support\Facades\Http; - public function table(Table $table): Table { return $table ->records(function (int $page, int $recordsPerPage): LengthAwarePaginator { - $response = Http::baseUrl('https://laravel-api.test/api') - ->get('posts', [ - 'page' => $page, - 'per_page' => $recordsPerPage, + $skip = ($page - 1) * $recordsPerPage; + + $response = Http::baseUrl('https://dummyjson.com') + ->get('products', [ + 'limit' => $recordsPerPage, + 'skip' => $skip, ]) ->collect(); return new LengthAwarePaginator( - items: $response['data'], - total: $response['meta']['total'], - perPage: $response['meta']['per_page'], - currentPage: $response['meta']['current_page'] + items: $response['products'], + total: $response['total'], + perPage: $recordsPerPage, + currentPage: $page ); }) ->columns([ TextColumn::make('title'), + TextColumn::make('category'), + TextColumn::make('price') + ->money(), ]); } ``` -On the backend, your Laravel API should receive the `page` and `per_page` parameters, apply pagination using [paginate()](https://laravel.com/docs/eloquent-resources#pagination) method, and return a [Resource Collection](https://laravel.com/docs/eloquent-resources#resource-collections) that includes the pagination metadata: - -```php -use App\Models\Post; -use Illuminate\Http\Request; -use Illuminate\Support\Facades\Route; - -Route::get('/posts', function (Request $request) { - $perPage = $request->query('per_page', 10); - - return Post::paginate($perPage) - ->toResourceCollection(); -}); -``` +`$page` and `$recordsPerPage` are automatically injected by Filament based on the current pagination state. +The calculated `skip` value tells the API how many records to skip before returning results for the current page. +The response contains `products` (the paginated items) and `total` (the total number of available items). +These values are passed to a `LengthAwarePaginator`, which Filament uses to render pagination controls correctly. - - + \ No newline at end of file From 4c7148682eb5297af77f3771a6c6988273d9dbe6 Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Tue, 15 Apr 2025 05:12:01 -0300 Subject: [PATCH 12/19] Adds explanation for API data retrieval in docs --- packages/tables/docs/09-static-data.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index 8b76e9f175..8b319f4fb0 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -448,6 +448,7 @@ public function table(Table $table): Table ]); } ``` +`get('products')` makes a `GET` request to [`https://dummyjson.com/product`](https://dummyjson.com/product). 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. ### External API Filtering @@ -613,7 +613,7 @@ If a category filter is selected, the request is made to `/products/category/{ca ### External API Pagination From 19a07fff3b44c53f6e50c401ee1e1229af05a908 Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Tue, 15 Apr 2025 08:18:45 -0300 Subject: [PATCH 15/19] Updates section headings --- packages/tables/docs/09-static-data.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index aa8233208c..e66b3c9b84 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -418,7 +418,7 @@ public function table(Table $table): Table } ``` -## Using an External API as a Table Data Source +## Using an external API as a table data source [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. @@ -476,7 +476,7 @@ TextColumn::make('category') You can use the [`formatStateUsing()`](columns/text#formatting) method to format the state of a text column without changing the state itself. -### External API Sorting +### External API sorting 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. @@ -518,7 +518,7 @@ public function table(Table $table): Table 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. -### External API Searching +### External API searching 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. @@ -560,7 +560,7 @@ public function table(Table $table): Table 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. -### External API Filtering +### External API filtering 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. @@ -616,7 +616,7 @@ If a category filter is selected, the request is made to `/products/category/{ca 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. -### External API Pagination +### External API pagination 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: From 907ad187e72f78ec39139a90bd08629ebad03d4d Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Tue, 15 Apr 2025 09:04:11 -0300 Subject: [PATCH 16/19] Updates column to display combined category and brand --- packages/tables/docs/09-static-data.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index e66b3c9b84..c5d8c63cbb 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -468,8 +468,14 @@ When working with the current record inside a column function, set the `$record` use Filament\Tables\Columns\TextColumn; use Illuminate\Support\Str; -TextColumn::make('category') - ->state(fn (array $record): string => Str::headline($record['category'])), +TextColumn::make('category_brand') + ->label('Category - Brand') + ->state(function (array $record): string { + $category = Str::headline($record['category']); + $brand = Str::title(data_get($record, 'brand', 'Unknown')); + + return "{$category} - {$brand}"; + }) ``` From 16cbd5c874710979ac612d34f1e6b72285f4bfbf Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Wed, 16 Apr 2025 09:39:54 +0100 Subject: [PATCH 19/19] Update 09-static-data.md --- packages/tables/docs/09-static-data.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/tables/docs/09-static-data.md b/packages/tables/docs/09-static-data.md index 01fb2c060f..c00fe9d2b1 100644 --- a/packages/tables/docs/09-static-data.md +++ b/packages/tables/docs/09-static-data.md @@ -459,7 +459,7 @@ public function table(Table $table): Table 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. -### Setting the state of a column using API data +#### Setting the state of a column using API data [Columns](#columns) map to the array keys returned by the `records()` function.