Today article we will explain and guide you to store data JSON in MySQL database Laravel 9. You will understand how to insert and JSON data and convert back from MySQL database to display back as JSON string using Laravel 9.
JSON is popular format which is lightweight data interchange format that uses human-readable text to store and transmit data objects contain key value pairs and arrays. Sometime you may not want to store as raw text or number but JSON fomat so, today below example will help you how to store JSON data in the MySQL database.
Let's see below example step by step to store JSON data in MySQL database.
We are going to download a fresh Laravel app by runing follow command, if you not yet install composer please read this from sctrach to install Laravel in your computer.
composer create-project laravel/laravel blog
To generate migration file at the same time generate the model, you may use the --migration
or -m
option
php artisan make:model News -m
Open file app -> models -> news.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
class News extends Model
{
use HasFactory;
/**
* @return response()
*/
protected $fillable = [
'title', 'tags'
];
/**
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute
*/
protected function tags(): Attribute
{
return Attribute::make(
get: fn ($value) => json_decode($value, true),
set: fn ($value) => json_encode($value),
);
}
}
Open file database -> migrations -> 2022_06_21_033722_create_news_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('news', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->json('tags')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('news');
}
};
After that make sure you have change database connection in .env file accordingly. Open command prompt and run the following command to create tables in database
php artisan migrate
Now we will add route in web.php to defined where URL point to Controller and function.
Open file routes -> web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsController;
/*
|--------------------------------------------------------------------------
| 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('/', [NewsController::class, 'index']);
This step, we are going to create controller named NewsController. Following command to create controller and update code as below.
php artisan make:controller NewsController
For testing I will create only one method with create a new record and display dd()
Dump and Die.
Open file app -> Http -> Controllers -> NewsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\News;
class NewsController extends Controller
{
/**
*
* @return response()
*/
public function index()
{
$input = [
'title' => 'Tiger and Lion are friend',
'tags' => [
'category' => 'Wild Life',
'description' => 'Lion walk to the moon',
'type' => 'Action'
]
];
$news = News::create($input);
dd($news ->tags);
}
}
Finally, run server development to start server project by following command
php artisan serve
Open URL http://localhost:8000/ you will see the output as below
Output:
array:3 [
"category" => "Wild Life"
"description" => "Lion walk to the moon"
"type" => "Action"
]
You might also like...
Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him