F
Filament7mo ago
AgonK

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
Dennis Koch
Dennis Koch7mo ago
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
AgonK
AgonKOP7mo ago
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
Dan Harrin
Dan Harrin7mo ago
You can override the page classes which hold all the logic for saving etc you can call services from places like handleRecordCreation()
AgonK
AgonKOP7mo ago
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
Dan Harrin
Dan Harrin7mo ago
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
AgonK
AgonKOP7mo ago
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
Dan Harrin
Dan Harrin7mo ago
throw a validation exception, which will halt the method?
AgonK
AgonKOP7mo ago
Sounds good! How can I throw? Inside the handle method ?
Dan Harrin
Dan Harrin7mo ago
yup throw ValidationException::withMessages([]) etc
Frans
Frans7mo ago
Looking forward on this! Thanks!
AgonK
AgonKOP7mo ago
@Dan Harrin this throw doesnt show any message like notification message. Did I miss something Throw break the request but message doesn’t appear
Dan Harrin
Dan Harrin7mo ago
try this instead
Notification::make()
->title('Message here')
->danger()
->send();

throw new Halt();
Notification::make()
->title('Message here')
->danger()
->send();

throw new Halt();
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
Want results from more Discord servers?
Add your server