In this tutorial you will learn about the Laravel 8 Create JSON File & Download From Text and its application with practical example.
In this Laravel 8 Create JSON File and Download From Text tutorial I will show you how to convert form data to JSON and download json file. I will also show you how to insert and download json format data into MySQL database. In this tutorial you will learn convert form data or text file data into json format and to save text file data from JSON data into MySQL database and download data from mysql database.
Laravel 8 Create JSON File & Download From Text
In this step by step tutorial I will guide you through how to save form or text data into database with json format and to download in laravel 8 application. Please follow the instruction given below:
- Step 1: Install Laravel Latest 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 Latest Setup
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel LaravelJson |
Step 2: Setup Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3: Migrate Database
Before you run php artisan migrate command go to app/providers/AppServiceProvider.php and update the below code into AppServiceProvider.php file:
1 2 3 4 5 6 |
use Illuminate\Support\Facades\Schema; function boot() { Schema::defaultStringLength(191); } |
Step 4: Generate migration and model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Test -m |
The above command will create a 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:
1 2 3 4 5 6 7 8 9 |
public function up() { Schema::create('tests', function (Blueprint $table) { $table->increments('id'); $table->string('token')->nullable(); $table->text('data')->nullable(); $table->timestamps(); }); } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Step 5: Add Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 |
use App\Http\Controllers\JsonController; Route::get('laravel-json', [JsonController::class, 'index']); Route::post('json-file-download', [JsonController::class, 'download']); |
Step 6: Create Controller
Now, lets create a controller named JsonController using command given below –
1 |
php artisan make:controller JsonController |
Now, go to app/http/controllers open JsonController and create two method. The first method is to display the form and second method convert form data to json format and save it to database:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php 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); Test::insert($test); $fileName = $test['token']. '_datafile.json'; File::put(public_path('/upload/json/'.$fileName),$test); return download(public_path('/upload/jsonfile/'.$fileName)); } } |
Step 7: Create Blade view
In this step we will create blade view file. Now, create a blade view name json_form.blade.php in resources/views directory. Put the below code here :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<!doctype html> <html lang="en"> <head> <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="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> <style> .error{ color:red; } </style> </head> <body> <div class="container"> <h2 style="margin-top: 10px;">Laravel Store Data To Json Format In Database</h2> <br> <br> @if ($message = Session::get('success')) <div class="alert alert-success alert-block"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> <br> @endif <form id="laravel_json" method="post" action="{{url('json-file-download')}}"> @csrf <div class="form-group"> <label for="formGroupExampleInput">Name</label> <input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="Please enter name"> </div> <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> <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> <div class="form-group"> <button type="submit" class="btn btn-success">Submit</button> </div> </form> </div> </body> </html> |
Step 8: Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/ |