How to load custom JSON files into Laravel's config

How to load custom JSON files into Laravel's config

Hi All, this is Adi, again with another Laravel blog post. This time I wanted to share my solution to loading custom settings from a JSON file into Laravel’s config. Firstly what’s the use case for this, there was 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 I was not able to persuade them to this option, so here’s my solution to how I did it.

Loading the Settings file

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

In the code below I load the settings.json that’s in the storage folder in my AppServiceProvider's boot method. But if you have a lot more logic happening in your service provider you can create a new one just for this purpose.

I check if the file exists, if so, I decode the JSON into a 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.

No alt text provided for this image


Writing the Settings file

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

In the below code, I show 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' => 'New Value']) and you could json_encode(config('settings')) and save the results to the file system.

No alt text provided for this image


Conclusion

I hope this gives you some idea of how to handle JSON config files in your Laravel app. The code example I gave you might be basic but you can extend this to your own needs. Be cautious since you’re reading and writing to the file system directly.

For more Laravel resources visit BestOfLaravel.com

Thank You
That’s all for now, this has been Adi.
If you are looking for a Freelance Web Developer you can contact me


要查看或添加评论,请登录

Adi SK的更多文章

  • My Journey with the Tech Stack ?? at Uzhavarbumi

    My Journey with the Tech Stack ?? at Uzhavarbumi

    Four years ago today, on 1/Jun/2019, I "officially" started a new freelance project. For us developers, the official…

    4 条评论
  • How to setup Specific Subdomain routing in Laravel

    How to setup Specific Subdomain routing in Laravel

    Hi all, this is Adi, again with another Laravel post. In one of my recent projects, I had to set up specific subdomain…

    1 条评论
  • Create custom Laravel Helper Methods

    Create custom Laravel Helper Methods

    Hi All, this is Adi, with another Laravel article. I wanted to take some time to explain what Laravel’s Helper methods…

  • What are Laravel Resource Controllers

    What are Laravel Resource Controllers

    Hi all, this is Adi again for a Laravel post. I recently found a lot of questions on StackOverflow asking what Laravel…

  • SimplestWeb Updated

    SimplestWeb Updated

    Hi All, I have some great news about Simplest Web. I’ve decided it’s time to take my freelance efforts to the next…

  • A recap of 2019

    A recap of 2019

    Hi all, this I Adi. 2019 has been a wonderful year for me, so I just wanted to take a moment to reflect and document…

  • How to setup Tailwind CSS with Parcel Bundler

    How to setup Tailwind CSS with Parcel Bundler

    Introduction Hi all, this is Adi. Today, I wanted to share with you my experience of how I set up Tailwind CSS with…

  • Reusing a Controller method for multiple actions in Laravel

    Reusing a Controller method for multiple actions in Laravel

    Hi All, this is Adi with another Laravel tutorial. This time, I wanted to cover my basic solution to a problem we might…

  • How to setup automatic db backup in Laravel

    How to setup automatic db backup in Laravel

    Hi All, this is Adi again with a Laravel related article. This time in a shorter format.

  • Launching Jobs by LaravelCollections.com

    Launching Jobs by LaravelCollections.com

    Hi All. I wanted to show you a service I have been working on for the past few weeks.

社区洞察

其他会员也浏览了