What is the best practice for adding constants in laravel? (Long List)
I use aliased class constants :
First, create your class that contain your constants : App/MyApp.php
for exemple
namespace App;
class MyApp {
const MYCONST = 'val';
}
Then add it to the aliased classes in the config/app.php
'aliases' => [
//...
'MyApp' => App\MyApp::class,
Finally use them wherever you like (controllers or even blades) :
MyApp::MYCONST
For most constants used globally across the application, storing them in config files is sufficient. It is also pretty simple
Create a new file in the config
directory. Let's call it constants.php
In there you have to return an array of config values.
return [
'options' => [
'option_attachment' => '13',
'option_email' => '14',
'option_monetery' => '15',
'option_ratings' => '16',
'option_textarea' => '17',
]
];
And you can access them as follows
config('constants.options');
// or if you want a specific one
config('constants.options.option_attachment');