CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

23 February, 2021

Laravel 8 Create JSON File & Download From Text

 Programing Coderfunda     February 23, 2021     Laravel, laravel-tutorial     No comments   

 Laravel 8 Create JSON File & Download From Text

Laravel 8 Create JSON File & Download From Text

 

Laravel 8 converts form-data or text file data to JSON format data and insert into MySQL database also download. In this tutorial, you will learn how to store text file data from JSON data into MySQL db and download it.

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.

This tutorial will guide you how to create form and store form or text data into database with json format and as well as how to download it in laravel 8 app.

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

  • 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, you need to download laravel fresh setup name Laravel Json to insert or store data into laravel DB app. So, use the below command and 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_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
Recommended:- Laravel 8 Ajax Post Form Data With Validation

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:

 
use Illuminate\Support\Facades\Schema;

function boot()
{
Schema::defaultStringLength(191);
}
Recommended:- Laravel 8 Auth Scaffolding using Jetstream

Step 4: Generate migration and model

In this step, you need 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:

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 you need to run the below command. It will create some tables in our database, so use the below command :

php artisan migrate
Recommended:- Laravel 8 Vue Js Infinity Page Scroll Example

Step 5: Add Route

In this step, you need 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:

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

In this step, you need to create one controller name JsonController.php. So use the below command and create a controller :

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;
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));
  }
 
}
Recommended:- Laravel 8 Send Mail using Queue Tutorial

Step 7: Create Blade view

In this step, you need to create one blade view name json.blade.php let go to resources/views and create one blade view file.

After create blade view file 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 - Tutsmake.com</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 - Tutsmake.com</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('store-json')}}">
      @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>
Recommended:- Laravel 8 QR Code Generator Tutorial Example

Step 8: Start Development Server

Finally, Run the following command 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.

 http://localhost:8000/
Or direct hit in your browser
http://localhost/LaravelJson/public
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Credit card validation in laravel
      Validation rules for credit card using laravel-validation-rules/credit-card package in laravel Install package laravel-validation-rules/cr...
  • Write API Integrations in Laravel and PHP Projects with Saloon
    Write API Integrations in Laravel and PHP Projects with Saloon Saloon  is a Laravel/PHP package that allows you to write your API integratio...
  • iOS 17 Force Screen Rotation not working on iPAD only
    I have followed all the links on Google and StackOverFlow, unfortunately, I could not find any reliable solution Specifically for iPad devic...
  • C++ in Hindi Introduction
    C ++ का परिचय C ++ एक ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग लैंग्वेज है। C ++ को Bjarne Stroustrup द्वारा विकसित किया गया था। C ++ में आने से पह...
  • Python AttributeError: 'str' has no attribute glob
    I am trying to look for a folder in a directory but I am getting the error.AttributeError: 'str' has no attribute glob Here's ...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (2)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda