Load custom JSON files into Laravel’s config

Poovarasu
2 min readAug 14, 2021

--

Here is how we can simply load JSON files in Laravel Config

First, let’s see how to load this file while the app initializes.

In the code below loaded the settings.json that's in the storage folder in AppServiceProvider's boot method.

I check if the file exists, if so, I decode the JSON into PHP array then add it to the app config. Now you are able to access all the key's from the settings file as config('settings.name') or whatever the key is.

Now let’s see how to edit or write to this file when you need to make new changes to the config.

Below code is an example of getting the whole settings file’s contents and saving it to the file. But you can customize this for your own need, let’s say you just need to edit one property, you can do this by updating your config like so config(['settings.name' => 'Updated Value']) and you could json_encode(config('settings')) and save the results to the file system.

I hope this gives you some idea of how to handle JSON config files in your Laravel app. But be cautious since you’re reading and writing to the file system directly.

--

--

Poovarasu

Senior PHP/Python Developer @ Mallow Technologies | Laravel & Django | Tech Enthusiast | Code Wizard | Forward-Thinking Techie