DEV Community

Rupadana
Rupadana

Posted on

How to use Filament Api Service Plugin

Installation

You can install the package via composer:

composer require rupadana/filament-api-service
Enter fullscreen mode Exit fullscreen mode

Usage

php artisan make:filament-api-service BlogResource
Enter fullscreen mode Exit fullscreen mode

Add this code to your routes file, example in routes/api.php

...
use App\Filament\Resources\BlogResource\Api;
...

BlogApiService::routes();
Enter fullscreen mode Exit fullscreen mode

and then you will got this routes:

  • [GET] '/api/blogs' - Return LengthAwarePaginator
  • [GET] '/api/blogs/1' - Return single resource
  • [PUT] '/api/blogs/1' - Update resource
  • [DELETE] '/api/blogs/1' - Delete resource

Im using "spatie/laravel-query-builder": "^5.3" to handle query and filtering. u can see "spatie/laravel-query-builder": "^5.3" https://spatie.be/docs/laravel-query-builder/v5/introduction

You can specified allowedFilters and allowedFields in your model

Example

class User extends Model {
    public static array $allowedFilters = [
        'name'
    ];

    public static array $allowedFields = [
        'name'
    ];
}
Enter fullscreen mode Exit fullscreen mode

Transform API Response

php artisan make:filament-api-transformer Blog
Enter fullscreen mode Exit fullscreen mode

it will be create BlogTransformer in App\Filament\Resources\BlogResource\Api\Transformers

next step you need to edit & add it to your Resource

    use App\Filament\Resources\BlogResource\Api\Transformers\BlogTransformer;

    class BlogResource extends Resource
    {
        ...
        public static function getApiTransformer() 
        { 
            return BlogTransformer::class; 
        }
        ...
    }
Enter fullscreen mode Exit fullscreen mode

Group Name & Prefix

You can edit prefix & group route name as you want, default im use model singular label;

    class BlogApiService extends ApiService
    {
        ...
        protected static string | null $groupRouteName = 'myblog';
        ...
    }
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
rupadana profile image
Rupadana

Hi, kindly leave a like and comment if you got new insight! 🔥