Constants play important role and many times you need some globally defined constants which can be use throughout the project. Laravel is most popular and full of features framework in PHP. It is easy in laravel to define constant and use in whole project.

Steps to create constant

STEP-1 Create a new file named constants.php inside config folder.

STEP-2 In this file you create array and add any number of elements. Here is an example of order stage

<?php
return [
    'ORDER_STAGES' => array(
        'processing'=>'Processing',
        'paid'=>'Paid',
        'ready'=>'Ready to Ship',
        'completed'=>'Completed'
    ),

That’s it.

Syntax

constants.<Variable_Name> 
Here Variable_Name is array element defined in constant.php file inside config folder.

How to use

These array defined in constant.php can be use throughout the project in controller and view (blade files)

To use in controller – config(“constants.ORDER_STAGES”)
To use in Blade View – {{ config(‘constants.ORDER_STAGES‘) }}

This is an easy way to use global array variable as constant


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *