DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From 13,000 to 20,000+ Endpoints: Architecting Forensics for the Remote Workforce
  • Give Your AI Assistant Long-Term Memory With perag
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

Trending

  • Logging What AI Agents Do in Salesforce: A Simple One-Object Audit Framework
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Deployment Lessons You Only Learn the Hard Way
  • 7 Technology Waves I’ve Seen in 30 Years of Software — Will AI Be the Next Real Transformation?
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Load Custom JSON Files Into Laravel's Config

How to Load Custom JSON Files Into Laravel's Config

By 
Adi Sk user avatar
Adi Sk
·
Jul. 07, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
10.6K Views

Join the DZone community and get the full member experience.

Join For Free

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? I was 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 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, I add it to the app config. Now, you are able to access all the keys from the settings file as config('settings.name') or whatever the key is.

Loading the settings file

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.

Writing the settings file

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

JSON File system Laravel

Published at DZone with permission of Adi Sk. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • From 13,000 to 20,000+ Endpoints: Architecting Forensics for the Remote Workforce
  • Give Your AI Assistant Long-Term Memory With perag
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook