Way To Load Custom JSON Files Into Laravel's Config

Hello Artisan, in this brand new tutorial i will show you how we can load custom json file in Laravel. Sometimes we need to load custom json file in our Laravel application. If you don't know that how to load custom json file in Laravel config then follow my tutorial. 

From this tutotial you will learn load json file in your laravel config. I am working on a client project, where they had a huge list of configurations in a JSON file, and I had to use them in the code. An ideal solution for this could be storing these settings in the database and query them on demand, but for making it more easier I was not able to persuade them to this option, so here’s my solution to how I did it. 

public function boot()
{
  if(file_exists(storage_path('settings.json'))){
       $settings = json_decode(file_get_contents(storage_path('settings.json')),true);
       config(['settings' => $settings]);
  }
}

 

Read also : Laravel Eloquent Global Scope Example Tutorial

 

 Now, you can access all the keys from the settings file as config('settings.name') or whatever the key is.

 

#laravel #laravel-8x #json