Load custom JSON files into Laravel’s config

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store