Hello all!
I'm using "services" (though they may not follow any specific pattern definition!) to move much of the logic from a controller to a different class.
This works pretty well, but I keep running into an issue where I have something like "user_id" passed in with a request as part of the form input. Or, sometimes a "account_id" will be passed in with a request, and I want to load the Account, and then get the Account's (hasOne) User.
Is there a way that is clean/great for dealing with this? The alternative being that right now my controller might pass all data onto a service, then the service has to get the AccountRepository and load the Account, and then from there get the UserRepository and load the User.
I've through about building a bunch of classes called something like UserResolver
- this class might have a method like
resolveUserFromAccountId($id)
or resolveUserFromUserId
or possibly even resolveUser
which would just take random input and it would do it's best to load the appropriate User given a particular Account ID or User Id, etc. The idea being I wouldn't have to repeat logic for figuring out how to load a particular Model given input.
Anyway, there is likely a much more elegant solution to this, but I'm not sure what at the moment.
I'd appreciate your advice/input!
<3
OK, I've read that this might be something of an Anti-Pattern (?) - how do you do it?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community