Lomkit / laravel-rest-api

Generate Api in seconds
https://laravel-rest-api.lomkit.com/
MIT License
298 stars 18 forks source link

Do you suggest to use an action if the mutate operation require more than two operations? #36

Closed rizaldywirawan closed 9 months ago

rizaldywirawan commented 10 months ago

Description

Hello,

Since we do not have discussion page yet, then I will ask here. Thank you for the package, seems better and more flexible than Laravel Orion.

I just want to ask, let's say I have a condition where when user register, the API endpoint will create the user and profile data (two different models), do you suggest we use an action to do that?

Thank you.

Regards, Rizaldy Wirawan

GautierDele commented 10 months ago

Hello @rizaldywirawan

Thanks for your interest, i'm not quite sure of what you are exactly trying to achieve but here's my point of view, don't hesitate to answer if thats not what you were looking for:

The mutate endpoint is made to alterate data, either creating, mutating, attaching or detaching. If your profile should be specified by the front end application, make it "requiredOnCreation". If its not, but required for your Application to work, specify it in an observer.

Actions are meant to execute mass operations like sending emails, archiving models / etc. You can use them to create the profile model you are talking about but i'm not sure this is what you want since it'll require two api calls. The only way it should be fine is when the user is the one wanting to make the operation 😄

I did not reproduce hooks from Orion but when srguing about your question, I think it could be good since hooks are API side only. In my first reflexion i thought that using hooks was a bad practice contrary to observers but might be not 😀