DEV Community

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

Collapse
tradjick profile image
tradjick

I use all of the above depending on usage and context. Regarding the db, I use both in class constants and enum fields within the model classes and migrations. I will have something like:

class Terminal extends Model
{
const STATE_REGISTERED='registered';
const STATE_UPDATE='update';
const STATE_LOCKED='locked';
const STATES=[self::STATE_REGISTERED,self::STATE_UPDATE,self::STATE_LOCKED];
...

and within the migration:
$table->enum('status',\App\Terminal::STATES);

The above means there is a singular authoritative source, that is detectable by the IDE, is contextual within the model, reduces record size for storage, speeds searches, and is enforced on the data storage level.

The only caveat with this technique is the migration changes if you change the authoritative class constant.