Laravel Create JSON File & Download From Text Example

By Hardik Savani November 5, 2023 Category : Laravel

In this example, I will show you laravel create json file. you will learn how to create json file and download laravel. you'll learn how to create json file from text in laravel. This post will give you simple example of laravel create json file in storage. Alright, let’s dive into the steps.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 version.

If you want create your own text file, html file, css file, php file, json file etc in Laravel. then we do not need to use any PHP library or Class because Laravel Framwork Provide its own "File" Facade. File Class through you can create your own html file, txt file, php file etc as you want. File Facade Class also provide several function like you can create file, remove file, update file etc. now i am giving you example of how to create json file in Laravel.

you also want to download file that you created now, i mean you want to give download file, then you can also give download that file using Laravel "Response" Class. in Laravel Response Class is use for give response or redirect something, one page to another page.so, basically how to give create file and download that file example below.

Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Route

Here, we will

In this is step we need to create route for create json file from text and download it. so open your "routes/web.php" file and add following route.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\JSONController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('download-json-file', [JSONController::class, 'index']);

Step 3: Create JSONController Controller

In this point, now we should create a new controller as JSONController. In this controller, we will add index method to create json file and response as download.

Let's update following code to your controller file:

app/Http/Controllers/JSONController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use File;

class JSONController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$data = json_encode(['Example 1','Example 2','Example 3',]);

$fileName = time() . '_datafile.json';

$fileStorePath = public_path('/upload/json/'.$fileName);

File::put($fileStorePath, $data);

return response()->download($fileStorePath);

}

}

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/download-json-file

Maybe it can help you.....

Tags :
Shares