Laravel 9 Create JSON File with Download & Save Database

Laravel 9 Create JSON File Example; In this tutorial you will learn how to convert form-data or text file data to JSON format data in laravel and insert into database with download functionality.

As well as how to convert string data or text file data into JSON format data and how to insert JSON data into MySQL using laravel. We will also learn how to download the file we create with the File facade. Not only but also we will check out the usage of the Response class; it mainly helps in downloading from the created file.

Laravel Create JSON File & Download From Text

Follow the following steps to create and download JSON file from text or form data in laravel app:

  • Step 1: Install Laravel App
  • Step 2: Connect Database to App
  • Step 3: Make Routes
  • Step 4: Create controller
  • Step 5: Create blade view
  • Step 6: Start Development Server

Install Laravel App

In the beginning, open a terminal window and use the suggested command to create a fresh laravel application:

composer create-project --prefer-dist laravel/laravel laravel-json

Go into the app:

cd laravel-json

Connecting Database to App

Next, insert database details in .env config file, it makes the laravel connection with the database:

 DB_DATABASE=database name here
 DB_USERNAME=database username here
 DB_PASSWORD=database password here

Make Routes

Next, get into the routes/web.php file, and add the following routes to web.php file:

use App\Http\Controllers\JsonController;
Route::get('laravel-json', [JsonController::class, 'index']);
Route::post('json-file-download', [JsonController::class, 'download']);

Create Controller

Now, generate a controller, this new controller will hold the create jsonfile from form-data programming logic.

php artisan make:controller JsonController 

Plus, open app/Http/Controllers/JSONController.php file and place the following code.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class JsonController extends Controller
   public function index()
      return view('json-form');
  public function download(Request $request)
      $data = $request->only('name','email','mobile_number');
      $data = json_encode($data);

      $fileName = time(). '_datafile.json';
      return download(public_path('/upload/jsonfile/'.$fileName));

Create Blade view

In this step, go to resources/views and create one blade view file json-form.blade.php, After that open resources/views/json-form.blade.php file and put the below code on it;

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <title>Laravel Store Data To Json Format In Database</title>
  <link rel="stylesheet" href="" />
   .error{ color:red; } 
<div class="container">
    <h2 style="margin-top: 10px;">Laravel Store Data To Json Format In Database</h2>
    @if ($message = Session::get('success'))
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>
          <strong>{{ $message }}</strong>
    <form id="laravel_json" method="post" action="{{url('json-file-download')}}">
      <div class="form-group">
        <label for="formGroupExampleInput">Name</label>
        <input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="Please enter name">
      <div class="form-group">
        <label for="email">Email Id</label>
        <input type="text" name="email" class="form-control" id="email" placeholder="Please enter email id">
      <div class="form-group">
        <label for="mobile_number">Mobile Number</label>
        <input type="text" name="mobile_number" class="form-control" id="mobile_number" placeholder="Please enter mobile number">
      <div class="form-group">
       <button type="submit" class="btn btn-success">Submit</button>

Start Your Application

Finally, Run the following command to start the development server. So use the PHP artisan serve command and start your server :

 php artisan serve

 If you want to run the project diffrent port so use this below command 

 php artisan serve --port=8080  

Now you are ready to run our laravel JSON data stored to database example so run the below command to quick run.

Or direct hit in your browser

In this laravel tutorial for storing JSON data in the database, you have successfully laravel project stored data in JSON format in our database. our examples run quickly.

Leave a Comment