Please forgive any ignorance on my part. Has anyone devised of a more reliable way (in my mind) to keep field names and table names consistent cross-project? It appears that I should in every place be repeating myself by giving a string that has a field name or a table name, when it would be better to reference a constant for this, or perhaps some property somewhere. A simple idea is:
const FIELD_ID = 'id'; const FIELD_HTML = 'html';
So instead of doing literal string I would be doing self::FIELD_HTML, or in referring to this class, Entry::FIELD_HTML.
Likewise, I don't know if there is a class for table fields like there is for the table models, that could in some way help with this?
I can't imagine that people haven't considered this and have reasons for either doing something like this or not doing it. I just think it's a disadvantage to have the references to these fields all over the project as simple strings that are repeated and could easily be mis-typed, and have no real object/class reference. Thanks for any input!
Define cross-project. Are you referring to keeping names consistent in the same project, or across multiple projects?
Either way seems like using constants is solving the wrong problem. If you're talking about the same project, you should be abstracting references to tables behind a repository, so you never need to define the correct table to use more than once. As far as field names, I guess you can define them all as constants, but it seems like you'd end up with this massive number of constant references that would lead to much more cognitive load than just remembering the correct field names.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community