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_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
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.
<?php
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';
File::put(public_path('/upload/json/'.$fileName),$data);
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">
<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>
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.
http://localhost:8000/
Or direct hit in your browser
http://localhost/LaravelJson/public
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.