how to integrate existing APIs with filament
I have existing API app that I use for mobile applications. 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 eloquent models. For example I want to send submit form to my API p.s
Route::post('/teachers', [TeacherController::class, 'store']);
and edit to
Route::put('/teachers/{id}', [TeacherController::class, 'update']);
is it possible ? if Yes how can I do it, because Im not familiar with filament.
12 Replies
Filament wasn’t made to work without Eloquent models. Some people fill the gap with the Sushi package by Caleb. We are trying to fix this with v4
Also I worked with eloquent models, but while inserting and editing data a have a lot of business logic behind it, and i create custom services and because of that I want to send request directly to my endpoint to solve the issue
Do you have any idea to do that ? @Dennis Koch
You can override the page classes which hold all the logic for saving etc
you can call services from places like handleRecordCreation()
Yes its work perfect with handleRecordCreation. But this method returns model instance. What if I need to get other type of response like status and message from api. How can I integrate it within this method @Dan Harrin
you can use a notification to do that, right?
you can send a notification and still return the model
alternatively you can step up a level and override the entire create() function, check out the parent class
Hmm no I think I don’t need for override entire method. What I need is to make compatible with APIs response conventions. If status is false I want to show the message property and tell the user the error
Without doing anything
How can I do that in short term @Dan Harrin
throw a validation exception, which will halt the method?
Sounds good! How can I throw? Inside the handle method ?
yup
throw ValidationException::withMessages([]) etc
Looking forward on this! Thanks!
@Dan Harrin this throw doesnt show any message like notification message. Did I miss something
Throw break the request but message doesn’t appear
try this instead
throwing a validation exception will work but you need to prefix field names with
data.
to get the errors in the form. notification is probably cleaner, and the Halt exception is Filament\Support\Exceptions\Halt
and Filament can catch that