Laravel 11 Create and Download JSON Text File

Laravel 11 create and download json text file; Through this tutorial, i am going to show you how to store text file data from JSON data into MySQL db and download it.

How to JSON file download And From Text File Or Form Data in Laravel 11?

Follow the below given steps to create and download json text file from form data or text data in Laravel 11 apps:

  • Step 1: Install Laravel 11 Setup
  • Step 2: Setup Database
  • Step 3: Generate migration and model
  • Step 4: Migrate Database
  • Step 5: Add Route
  • Step 6: Create controller
  • Step 7: Create blade view
  • Step 8: Start Development Server

Step 1: Install Laravel 11 Setup

Run following command to download fresh new laravel setup:

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

Step 2: Setup Database

In this step, add the database details in the .env file. So Navigate to your project root directory and open .env file. Then set up database credential into it:

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

Step 3: Migrate Database

Visit to app/providers/AppServiceProvider.php and update the below code into AppServiceProvider.php file:

 use Illuminate\Support\Facades\Schema;

 function boot()

Step 4: Generate migration and model

To generate one migration file with create one model name Test using the below command :

php artisan make:model Test -m

After creating the model and migration file. Go to app/database/migration and find the migration file name create_tests_table.php and update the following code into it:

   public function up()
        Schema::create('tests', function (Blueprint $table) {

Now you need to run the below command. It will create some tables in our database, so use the below command :

php artisan migrate

Step 5: Add Route

To create two routes in web.php file for one is display form and second route is store data in json to mysql database . Go to app/routes/web.php file and create below routes here:

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

Step 6: Create Controller

To create one controller name JsonController.php. So run the below command on command prompt:

php artisan make:controller JsonController 

After successfully create controller go to app/http/controllers open JsonController and create two method here. First method display the form and second method convert laravel string form data to json format and save it to database:

Note that, if you are using laravel 8.x version, so you should use Model on your controller file like this:

use App\Models\Test;
namespace App\Http\Controllers;
use Redirect,Response;
use Illuminate\Http\Request;
use App\Models\Test;
class JsonController extends Controller
   public function index()
      return view('json_form');
  public function download(Request $request)
      $data = $request->only('name','email','mobile_number');
      $test['token'] = time();
      $test['data'] = json_encode($data);
      $fileName = $test['token']. '_datafile.json';
      return download(public_path('/upload/jsonfile/'.$fileName));

Step 7: Create Blade view

To create one blade view name json.blade.php let go to resources/views and create one blade view file:

<!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>

Step 8: Start Development Server

Run the following command on command prompt to to start 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 datbase example so run below command to quick run.

Or direct hit in your browser


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

Recommended Laravel Tutorials

Leave a Comment