-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Add support for casting pure enums to integers instead of strings #53358
base: 11.x
Are you sure you want to change the base?
[Feature] Add support for casting pure enums to integers instead of strings #53358
Conversation
The con would be a really really big con for me. I don't think devs would expect to break their applications by reordering their enum cases. Also, if I'm not mistaken and assuming devs won't change the order, reading strings from the DB will still work but writing back will store an integer (or rather a digit since the DB column still is a varchar for example). However, I think this is a huge breaking change. Other apps may use the same DB or an API may return the values from the DB. Now they get "1" instead of "admin". |
@dennisprudlo I do agree with you on the first point, but how often actually the order of enum cases are changed, and also the storage of different types in the same column is a big disadvantage in the case of using the same db or an api outside the Laravel application, I don't have a solution for these but I'm open to suggestions. |
If you want to store an enum as an integer, I suggest using integer backed enums. Reordering would make it less likely one changes the values... |
I sometimes add a case and place it in alphabetical order or in order of significance. Read, Execute would become Read Write Execute if I added the Write case functionality later on. |
Maybe a better idea is to make an interface called CastsToIntegers and implement it in enums where I would like to apply this behaviour |
That is when you choose for integer backed enums ... |
[Feature] Add support for casting pure enums to integers instead of strings
This PR modifies the default behavior of storing pure enum values as strings to storing them as integers. The motivation behind this change is that, in most cases I use enums as casts I use integer backed enums —which typically requires maintaining specific value for each enum case— because int columns are more effiecent in storing and indexing, and don't care what is the value of each enum. This PR takes care of giving each case a unique value and it was inspired by implicit enum values in C++.
Cons
Enum::cases()
method which also depends on the order of the cases in the enum.Changes Made
getStorableEnumValue
method in theHasAttributes
trait to return an integer instead of a string.getEnumCaseFromValue
method in theHasAttributes
trait to interpret enum values as integers or strings for old values.Compatibility
This change is expected to be backward-compatible, If you can think of a scenario where this might introduce a breaking change, please let me know.
Tests
Example
Here’s an example to illustrate the change:
Before:
Using a pure enum cast in the model:
After:
With this PR, using the same pure enum cast will store integer values instead:
Notes
This change makes it easier to use pure enums in the casts without needing to define explicit integer-backed values for each case.