Replies: 2 comments 8 replies
-
You could exclude the optional fields from UserData and instead create an additional data object which extends UserData with the additional fields which are only present after persisting. |
Beta Was this translation helpful? Give feedback.
-
Sorry to dig up an old thread, but the issues mentioned in this discussion are very close to the initial pains I'm having moving from As mentioned in the original post, I also provide a unique id to hide the internal auto incrementing ids, initially I had this as optional When using With the above two issues, it was clear I needed to have separate Data objects for Requests and Resources, which I refactored to. I tried to use the Request Data object for both create and update, but now I have hit an issue where the update needs to ignore the route param, and create doesn't have a route param. Do we split the Data objects as well? Should we have Perhaps it's better to leave the complex (non-primitive) validation rules to the What are the best practices when implementing Laravel Data into a big code base? Are there any Open Source examples that we should follow? I've read Laravel Beyond CRUD, and re-read after version 2 was released, but it only gives an intro to Laravel Data. The example repo attached to that course uses the now-deprecated DTO package. Any guidance here would be much appreciated, as I'm keen to implement this powerful package, I just need to workout the tradeoffs before a huge refactor. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Refactoring an existing codebase from using Laravel's
FormRequest
combined withJsonResponse
to using this package'sData
objects, I'm still struggling a lot finding the best way to do it, and I was wondering whether someone on here had some good pattern they use, trying to always ensure we have valid data.Let's consider we have the following requirements.
In our
RegisterUserController
we are expectingUserData
. The three fields that we require areemail
,first_name
andlast_name
. Since we will send an email verification link, we do not need any more data at this point. So we started by building our Data model as follows:So far so good: when using it in our controller to register our new user we can keep everything very tidy:
Our action returns a
User
model, so we now want to have our controller return the$user
as a JsonResponse using our sameUserData
model. In our API, we have some rules on how we always structure our models, and basically we always want our models to have the following structure:Applying this to our UserData, we want to be able to return it as a JsonResponse and have the following payload:
So it's clear we have to extend our
UserData
model. But since we want to still being able to useUserData
for accepting an incomplete model when creating a new user, we need to make these new properties optional:As you can see, we introduced a new
ModelMetaData
allowing us to reuse the meta bit of any model in its own Data definition:Okay. Last step, we need to add
fromModel()
method to our Data definitions allowing us to create the Data objects from our Model; we do it first onModelMetaData
:And of course we need to support
fromModel
on our actualUserData
as well:Okay great, so I can now use the UserData to also return it from my Controller:
Now I'm not entirely happy with this yet, as for
UserData
returned from the controller, I'm not really validating that all the attributes that we set in the fromModel method are correct.$id
and$meta
are set only optionally here.I know that this is supposed to be used by backend code only, so we should basically be able to trust it, and we'll cover it in our testing too, but I'm still struggling a bit with this. How do others do this? Do I have to live with this? Or is there a nice pattern that I could apply to ensure my data is always validated?
I realise I'm probably overthinking this, as we didn't validate the JsonResponses either when we were using them. But we just want to make the most of using this beautiful package from the start. Maybe I'm missing the obvious here. My main concern is living with the optional attributes.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions