Laravel create a json file and append content to it.

In this post, I would like to show you creating a JSON file and appending content to it whenever somebody submits a form. For this tutorial, I am gonna use Laravel 5.6 version.

Let’s create a brand new Laravel project via Composer Create-Project command. lets issue following command from your terminal:

It will create a directory called jsonAppendDemo and download all the Laravel project files and its dependencies.

After completing setup process, create a view file called contact_form.blade.php in resources/views directory with following HTML.

Create a controller called ContactController.php with below command:

After creating controller lets define following methods, index is to show the contact form and store method is to handle the submitted data.

All set lets define routes to access the form and to process the submitted data in your web.php route file.

Now start the php server with below command

Once it is started, if you visiting http://localhost:8000, you should see the following page in your browser

Laravel create a json file and append content to it.

Once you submit the form data will be stored in the project_root/storage/app/data.json file.
Here is my data.json file.

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

1 reply on “Laravel create a json file and append content to it.”


Good example code demo,

I think you forgot to include this line in the ContactController.php :

use IlluminateSupportFacadesStorage;

Youness from team

Leave a Reply

Your email address will not be published. Required fields are marked *