DEV Community

Discussion on: Alternatives to using a constants.php file in your Laravel project

Collapse
 
mattdaneshvar profile image
Matt Daneshvar • Edited

I think it really comes down to the context and the specific reason behind each group of constants in an app. I don't see ENUMs as constants in the database, but rather as alternatives to having certain constants in the first place.

For example, I spoke to a friend whose constants file looked like this:

const ADMIN_ROLE = 1;
const EDITOR_ROLE = 2;
const PUBLISHER_ROLE = 3;
Enter fullscreen mode Exit fullscreen mode

His justification for using these was so that:

  • He could store integers (as a somewhat lighter / quicker type) instead of strings
  • He had a reference so that he didn't mess up his data with arbitrary values

He later removed his constants and used an ENUM database column. In his case:

  • His database still stores a small numeric value behind the scenes
  • He gets data integrity at database level (Arbitrary strings won't be stored... Even though ENUM behaviour is quite crappy in handling invalid values).
  • He deals with simple readable string values (e.g. 'admin' and 'editor') in his app without needing to define a set of constants.

It's hard to say if this is the right way, but I guess it's an alternative approach for him to address his goals.

The biggest drawback about database ENUMs I can think of is the fact that the app (and the IDE) won't have any idea about the ENUMs in your database, which would mean no autocompletion in the IDE and no integrity checks at the application level.

Are there other drawbacks you have mind? I think I should eventually add more details to this article to highlight the benefits and drawbacks of each approach better.

Collapse
 
vladi160 profile image
vladi160

I mean:

  1. a query just for config values. One more request for a data, which is static, but in DB is dynamic. You can store it in any kind of a file like .json, .txt, etc, even you can create your own format and there is .env.

  2. I guess, it will be hard to debug, if u use some DB values and thread them as constants or a config.