Skip to main content
Category:

Laravel Store JSON Format Data in Database

 

Create Model and migration

Now generate a model and migration file just running the below command.

php artisan make:model Post -m

app/database/migration

public function up()
 {
     Schema::create('posts', function (Blueprint $table) {
         $table->increments('id');
         $table->json('data')->nullable();
         $table->timestamps();
     });
 }

After creating and updating the code in your migration file now run the below command and check your database where your show a posts table is created along with users and reset_password tables.

php artisan migrate

 

Create Controller

Now create a new controller just running the below command.

php artisan make:controller PostController 

After running above command put the below command on it.

 
<?php
 
namespace App\Http\Controllers;
use App\Post;
use Redirect,Response;
use Illuminate\Http\Request;
 
class PostController extends Controller
{
   public function index()
   {
      return view('create');
   }  
 
  public function store(Request $request)
  {
      $data = json_encode($request);
      Post::create($data);

      return back()->withSuccess('Data successfully store in json format');
  }
 
}

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook