skip to Main Content

I have a production ready API with laravel 11 and I want to integrate filament for some pretty views, but I want to integrate filament (create,update,list) with my existing API and not to default filament CRUD operation directly with models. For example I want to send create submit form to my API
p.s

Route::post('/teachers', [TeacherController::class, 'store']);

and edit to

Route::put('/teachers/{id}', [TeacherController::class, 'update']);

How can I integrate it ?

Thanks you anyway!

I use filament for the first time and I have no idea how to customize filament with existing API endpoints

please do not post any solution from chatGPT

3

Answers


  1. Maybe something like this ? You can customize your actions:

    use FilamentForms;
    use FilamentResourcesForm;
    use IlluminateSupportFacadesHttp;
    
    class TeacherResource extends Form
    {
        public static function form(Form $form): Form
        {
            return $form
                ->schema([
                    // Define your form fields here
                    FormsComponentsTextInput::make('name')->required(),
                    FormsComponentsTextInput::make('email')->email()->required(),
                ])
                ->submit(function (array $data) {
                    if ($this->record->id) {
                        // Update logic
                        Http::put(url('/api/teachers/' . $this->record->id), $data);
                    } else {
                        // Create logic
                        Http::post(url('/api/teachers'), $data);
                    }
    
                    return redirect()->route('filament.resources.teachers.index');
                });
        }
    }
    
    Login or Signup to reply.
  2. you can change the create/update process, put your logic to call the existing API instead of actually creating the record using Eloquent

    https://filamentphp.com/docs/3.x/panels/resources/creating-records#customizing-the-creation-process

    https://filamentphp.com/docs/3.x/panels/resources/editing-records#customizing-the-saving-process

    Login or Signup to reply.
  3. I would like to mention that filament has two types of resources

    1. default resource where you use the command : php artisan make:filament-resource Customer as an example
    2. simple resource where you use : php artisan make:filament-resource Customer --simple

    you can find more about that in the document filament documentation get started

    assuming that you are working on the first type of resource, you would likely have a resource structure like this
    enter image description here

    so to customize the creation process go to App/Filament/Resource/PostResource/CreatePost

    add the method

            protected function handleRecordCreation(array $data): Model
            {
                        // Your logic here .......
                        Http::post(url('/api/teachers'), $data);
                      //reutn $model
    
            }
    

    then add method to handle redirect

    protected function getRedirectUrl(): string
    {
        return $this->getResource()::getUrl('index');
    }
    

    this is the way mentioned in the documentation check here

    to handle the edit action go to App/Filament/Resource/PostResource/EditPost as example

    protected function handleRecordUpdate(Model $record, array $data): Model
    {
        //your logic here
        Http::put(url('/api/teachers/' . $this->record->id), $data);
        return $record;
    }
    

    handle the edit action redirect using the method

    protected function getRedirectUrl(): string
    {
        return $this->getResource()::getUrl('index');
    }
    

    and again you can find all of these in the documentation cech here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search