How to Define Constant Variable in Laravel 6?

How to Define Constant Variable in Laravel 6?

In this tutorial, we will learn how to define constant variable in laravel 6 application. We can easily create laravel 6 constants variable. You can define constants variable with string value, integer value, array value and you can access for all controller, all views, all blade files, middleware too in Laravel 6 using config.

In this tutorial, we will learn how to define constant variable in laravel 6 application. We can easily create laravel 6 constants variable. You can define constants variable with string value, integer value, array value and you can access for all controller, all views, all blade files, middleware too in Laravel 6 using config.

We must be require to define some global value for application like number of pagination records, user type, site url etc. If you are working with small project then i will suggest to create global config file for define constants variable.

In this example we will create one global config file will all define default variable with values. So you can use that variable from anywhere in project. you can also check bellow screen shot for file location.

Create Config File

We need to create global.php config file with constants variable value and you can also define array value like as bellow:

config/global.php

<?php



return [



    'pagination_records' => 10,



    'user_type' => ['User', 'Admin'],

]



?>

Use Constant Variable

You can easily get value using config() helper. you can see bellow example:

routes/web.php

Route::get('get-user-type', function()

{

    dd(config('global.user_type'));

});

Read Also:Laravel 6 Dropzone Image Upload Tutorial

Route::get('get-user-type', function()

{

    dd(config('global.pagination_records'));

});

I hope it can help you....

laravel php

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→

Some of the most frequent how tos in Laravel

Spread the loveHow to get relationship from relationship using With() in Laravel Some times there are cases where you want to get relationship from relationship in Laravel, that can be achieved via following: How to create multiple where clauses in…Read More→

10 Laravel Quick Tips

Spread the loveTip 1. Controllers Having Single Action In some situations you need a single action in a controller, if this is the case in Laravel you can achieve it by __invoke() method. Routes: Artisan command to generate this controller:…Read More→

How to Install Laravel PHP Framework with Nginx on Ubuntu 20.04

Laravel is the most popular, free, and open-source PHP framework in the world, known for its expressive and elegant syntax. Laravel is accessible, powerful, and offers some of the best web development tools required for large, robust, and modern applications. In this article, you will learn how to install the Laravel PHP Framework on Ubuntu 20.04 server running on the Nginx web server.