In this post, I am going to explain “how to define constant variable in laravel”. we can create any type constant variable like array, string, Integer etc.
And can access any where in application like view, Controller, middle-ware etc.
Many times we require to define some global value for application like user type, site URL, success message, error message etc.
In this post we will create one global config file and define all default variable with values in created file. After that we can use that variable from anywhere in project.
Step 1:- Create Config File
first we need to create a file in config folder and then define constant variable and value in that file.
Create global.php file in config folder and paste below code.
'user_type' => ['Publisher', 'Admin'],
'success_msg' => "Record Inserted successfully",
'error_msg' => "Something went wrong"
Step:2 ) How to use Constant Variable, we can easly get config value using config() helper like:
$msg = config('global.success_msg');
$userType = config('global.user_type');
I hope you might have understood it properly, keep reading our other blog posts for more coding tricks.