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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Scaling Site Reliability Engineering (SRE) Teams the Right Way
  • Managing Data Residency, the Demo
  • Integrating AWS With Salesforce Using Terraform
  • MLOps: Definition, Importance, and Implementation

Trending

  • Scaling Site Reliability Engineering (SRE) Teams the Right Way
  • Managing Data Residency, the Demo
  • Integrating AWS With Salesforce Using Terraform
  • MLOps: Definition, Importance, and Implementation
  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

Adi Sk user avatar by
Adi Sk
·
Jul. 07, 20 · Tutorial
Like (2)
Save
Tweet
Share
9.18K 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.

Trending

  • Scaling Site Reliability Engineering (SRE) Teams the Right Way
  • Managing Data Residency, the Demo
  • Integrating AWS With Salesforce Using Terraform
  • MLOps: Definition, Importance, and Implementation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: