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');
}
}