How to Define Constant Variable in Laravel

Hello Geek, Creating global variables that can be used throughout your project is frequently required. For example, if you are working in the admin section and need to submit one record per page to the paginate() method to expose one record at a time everywhere you go, you can specify them in a single file and use them everywhere you need them instead of manually passing them.

1. Make a configuration file.

<?php
   
return [
  'pagination'=>[
     'perPage'=>25
  ]
]
  
?>

2. Make use of Constant Variable

You can easily get value using config() helper. you can see this in the example:

<?php

Route::get('/users', function(){

    return config('global.pagination.perPage');
});

All the best nerd!

Leave a Reply