Uploaded by m.ohamed.khalid

JSON MySQL

advertisement
How To Work with JSON in MySQL | DigitalOcean
2 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
3 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
4 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
5 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
6 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
7 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
8 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
9 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
10 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
11 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
12 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
13 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
14 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
15 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
16 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
17 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
18 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
19 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
20 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
21 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
e_store
brands
categories
products
e_store
CREATE DATABASE IF NOT EXISTS `e_store`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
SET default_storage_engine = INNODB;
brands
categories
id
name
brands
CREATE TABLE `e_store`.`brands`(
`id` INT UNSIGNED NOT NULL auto_increment ,
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
22 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
`name` VARCHAR(250) NOT NULL ,
PRIMARY KEY(`id`)
);
categories
CREATE TABLE `e_store`.`categories`(
`id` INT UNSIGNED NOT NULL auto_increment ,
`name` VARCHAR(250) NOT NULL ,
PRIMARY KEY(`id`)
);
brands
INSERT INTO `e_store`.`brands`(`name`)
VALUES
('Samsung');
INSERT INTO `e_store`.`brands`(`name`)
VALUES
('Nokia');
INSERT INTO `e_store`.`brands`(`name`)
VALUES
('Canon');
categories
INSERT INTO `e_store`.`categories`(`name`)
VALUES
('Television');
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
23 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
INSERT INTO `e_store`.`categories`(`name`)
VALUES
('Mobile Phone');
INSERT INTO `e_store`.`categories`(`name`)
VALUES
('Camera');
products
id
name brand_id
category_id
attributes
CREATE TABLE `e_store`.`products`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(250) NOT NULL ,
`brand_id` INT UNSIGNED NOT NULL ,
`category_id` INT UNSIGNED NOT NULL ,
`attributes` JSON NOT NULL ,
PRIMARY KEY(`id`) ,
INDEX `CATEGORY_ID`(`category_id` ASC) ,
INDEX `BRAND_ID`(`brand_id` ASC) ,
CONSTRAINT `brand_id` FOREIGN KEY(`brand_id`) REFERENCES `e_store`.`brands`(`id`) ON DELETE RESTRICT
CONSTRAINT `category_id` FOREIGN KEY(`category_id`) REFERENCES `e_store`.`categories`(`id`) ON
);
brand_id
brands
category_id
categories
attributes
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
24 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
attributes
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
25 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
products
INSERT INTO
VALUES
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Prime' ,
'1' ,
'1' ,
'{"screen": "50 inch", "resolution": "2048 x 1152 pixels", "ports": {"hdmi": 1, "usb": 3}, "speakers": {"left": "
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
26 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
VALUES(
'Octoview' ,
'1' ,
'1' ,
'{"screen": "40 inch", "resolution": "1920 x 1080 pixels", "ports": {"hdmi": 1, "usb": 2}, "speakers": {"left": "
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Dreamer' ,
'1' ,
'1' ,
'{"screen": "30 inch", "resolution": "1600 x 900 pixles", "ports": {"hdmi": 1, "usb": 1}, "speakers": {"left": "1
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Bravia' ,
'1' ,
'1' ,
'{"screen": "25 inch", "resolution": "1366 x 768 pixels", "ports": {"hdmi": 1, "usb": 0}, "speakers": {"left": "5
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
27 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
`attributes`
)
VALUES(
'Proton' ,
'1' ,
'1' ,
'{"screen": "20 inch", "resolution": "1280 x 720 pixels", "ports": {"hdmi": 0, "usb": 0}, "speakers": {"left": "5
);
JSON_OBJECT
JSON_OBJECT
JSON_OBJECT(key1, value1, key2, value2, ... key(n), value(n))
JSON_OBJECT
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
28 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Passion' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA") ,
"body" ,
"6.11 x 3.59 x 0.46 inches" ,
"weight" ,
"145 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
29 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
"Android Jellybean v4.3"
)
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Emotion' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "EVDO") ,
"body" ,
"5.50 x 2.50 x 0.50 inches" ,
"weight" ,
"125 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"5.00 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android KitKat v4.3"
)
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
30 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
)
VALUES(
'Sensation' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "HSPA" , "EVDO") ,
"body" ,
"4.00 x 2.00 x 0.75 inches" ,
"weight" ,
"150 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"3.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Lollipop v4.3"
)
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Joy' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("CDMA" , "HSPA" , "EVDO") ,
"body" ,
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
31 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
"7.00 x 3.50 x 0.25 inches" ,
"weight" ,
"250 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"6.5 inches" ,
"resolution" ,
"1920 x 1080 pixels" ,
"os" ,
"Android Marshmallow v4.3"
)
);
JSON_ARRAY
JSON_MERGE_PRESERVE
JSON_MERGE_PATCH
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
32 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
JSON_MERGE
'JSON_MERGE' is deprecated and will be removed in a future release. Please use JSON_MERGE_PRESERVE/JSON_MERGE_
JSON_MERGE_PRESERVE
JSON_MERGE_PRESERVE
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE_PRESERVE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
INSERT INTO `e_store`.`products`(
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
33 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Runner' ,
'3' ,
'3' ,
JSON_MERGE_PRESERVE(
JSON_OBJECT("sensor_type" , "CMOS") ,
JSON_OBJECT("processor" , "Digic DV II") ,
JSON_OBJECT("scanning_system" , "progressive") ,
JSON_OBJECT("mount_type" , "PL") ,
JSON_OBJECT("monitor_type" , "LED")
)
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Traveler' ,
'3' ,
'3' ,
JSON_MERGE_PRESERVE(
JSON_OBJECT("sensor_type" , "CMOS") ,
'{"processor": "Digic DV II"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
34 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Walker' ,
'3' ,
'3' ,
JSON_MERGE_PRESERVE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV I"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LED"}'
)
);
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Jumper' ,
'3' ,
'3' ,
JSON_MERGE_PRESERVE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV I"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
35 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
JSON_MERGE_PRESERVE
JSON_OBJECT
JSON_MERGE_PRESERVE
network
SELECT JSON_MERGE_PRESERVE(
'{"network": "GSM"}' ,
'{"network": "CDMA"}' ,
'{"network": "HSPA"}' ,
'{"network": "EVDO"}'
);
{"network": ["GSM", "CDMA", "HSPA", "EVDO"]}
JSON_TYPE
SELECT JSON_TYPE(attributes) FROM `e_store`.`products`;
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
36 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
OBJECT
WHERE
$
JSON_EXTRACT
SELECT
*
FROM
`e_store`.`products`
WHERE
`category_id` = 1
AND JSON_EXTRACT(`attributes` , '$.ports.usb') > 0
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
37 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
AND JSON_EXTRACT(`attributes` , '$.ports.hdmi') > 0;
JSON_EXTRACT
attributes
$
$.ports.usb
$.ports.hdmi
>
JSON_EXTRACT
->
->
SELECT
*
FROM
`e_store`.`products`
WHERE
`category_id` = 1
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
38 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
AND `attributes` -> '$.ports.usb' > 0
AND `attributes` -> '$.ports.hdmi' > 0;
JSON_INSERT JSON_REPLACE
JSON_INSERT
JSON_SET
chipset
UPDATE `e_store`.`products`
SET `attributes` = JSON_INSERT(
`attributes` ,
'$.chipset' ,
'Qualcomm'
)
WHERE
`category_id` = 2;
$.chipset
chipset
SELECT
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
39 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
*
FROM
`e_store`.`products`
WHERE
`category_id` = 2
JSON_REPLACE
chipset
UPDATE `e_store`.`products`
SET `attributes` = JSON_REPLACE(
`attributes` ,
'$.chipset' ,
'Qualcomm Snapdragon'
)
WHERE
`category_id` = 2;
JSON_SET
body_color
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
40 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
UPDATE `e_store`.`products`
SET `attributes` = JSON_SET(
`attributes` ,
'$.body_color' ,
'red'
)
WHERE
`category_id` = 1;
JSON_INSERT
JSON_REPLACE
JSON_SET
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
41 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
JSON_REMOVE
DELETE
JSON_REMOVE
JSON_REMOVE
mount_type
UPDATE `e_store`.`products`
SET `attributes` = JSON_REMOVE(`attributes` , '$.mount_type')
WHERE
`category_id` = 3;
JSON_REMOVE
DELETE
DELETE
JSON_EXTRACT
LIKE
DELETE FROM `e_store`.`products`
WHERE `category_id` = 2
AND JSON_EXTRACT(`attributes` , '$.os') LIKE '%Jellybean%';
JSON_EXTRACT
LIKE
os
DELETE
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
42 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
Jellybean
$ composer create-project laravel/laravel estore-example
$ cd estore-example
.env
DB_DATABASE DB_USERNAME
brands categories
DB_PASSWORD
products
create_brands
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
43 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
$ php artisan make:migration create_brands
create_brands.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateBrands extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('brands', function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
44 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
*/
public function down()
{
Schema::dropIfExists('brands');
}
}
create_categories
$ php artisan make:migration create_categories
create_categories.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategories extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
45 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
create_products
$ php artisan make:migration create_products
create_products.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProducts extends Migration
{
/**
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
46 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->unsignedInteger('brand_id');
$table->unsignedInteger('category_id');
$table->json('attributes');
$table->timestamps();
// foreign key constraints
$table->foreign('brand_id')->references('id')->on('brands')->onDelete('restrict')->onUpdate
$table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict'
// indexes
$table->index('brand_id');
$table->index('category_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
$table->json('attributes');
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
47 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
json
brands
attributes
categories
products
Brand
$ php artisan make:model Brand
Brand.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Brand extends Model
{
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
48 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
use HasFactory;
// A brand has many products
public function products(){
return $this->hasMany('Product')
}
}
Category
$ php artisan make:model Category
Category.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
// A category has many products
public function products(){
return $this->hasMany('Product')
}
}
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
49 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
Product
$ php artisan make:model Product
Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
public $timestamps = false;
// Cast attributes JSON to array
protected $casts = [
'attributes' => 'array'
];
// Each product has a brand
public function brand(){
return $this->belongsTo('Brand');
}
// Each product has a category
public function category(){
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
50 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
return $this->belongsTo('Category');
}
}
$casts
attributes
array
attributes
$ php artisan make:controller CameraController
CameraController.php
<?php
namespace App\Http\Controllers;
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
51 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
use Illuminate\Http\Request;
class CameraController extends Controller
{
// creates product in database
// using form fields
public function store(Request $request){
// create object and set properties
$camera = new \App\Models\Product();
$camera->name = $request->name;
$camera->brand_id = $request->brand_id;
$camera->category_id = $request->category_id;
$camera->attributes = [
'processor' => $request->processor,
'sensor_type' => $request->sensor_type,
'monitor_type' => $request->monitor_type,
'scanning_system' => $request->scanning_system,
];
// save to database
$camera->save();
// show the created camera
return view('product.camera.show', ['camera' => $camera]);
}
}
store
new.blade.php
resources/views/product/camera
<form method="POST" action="/product/camera/store">
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
52 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
@csrf
<table>
<tr>
<td><label for="name">Name</label></td>
<td><input id="name" name="name" type="text"></td>
</tr>
<tr>
<td><label for="brand-id">Brand ID</label></td>
<td>
<select id="brand-id" name="brand_id">
<option value="1">Samsung</option>
<option value="2">Nokia</option>
<option value="3">Canon</option>
</select>
</td>
</tr>
<tr>
<td><label for="attributes-processor">Processor</label></td>
<td><input id="attributes-processor" name="processor" type="text"></td>
</tr>
<tr>
<td><label for="attributes-sensor-type">Sensor Type</label></td>
<td><input id="attributes-sensor-type" name="sensor_type" type="text"></td>
</tr>
<tr>
<td><label for="attributes-monitor-type">Monitor Type</label></td>
<td><input id="attributes-monitor-type" name="monitor_type" type="text"></td>
</tr>
<tr>
<td><label for="attributes-scanning-system">Scanning System</label></td>
<td><input id="attributes-scanning-system" name="scanning_system" type="text"></td>
</tr>
</table>
<input name="category_id" value="3" type="hidden">
<button type="submit">Submit</button>
</form>
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
53 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
brand_id
select
option
category_id
id
routes/web.php
// ...
use App\Http\Controllers\CameraController;
Route::get('/product/camera/new', function() {
return view('product/camera/new');
});
Route::post(
'/product/camera/store',
[CameraController::class, 'store']
);
$ php artisan serve
localhost:8000/product/camera/new
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
54 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
$casts
Product
CamerasController
<?php
// ...
class CameraController extends Controller
{
// ... store ...
// fetches a single product
// from database
public function show($id){
$camera = \App\Models\Product::find($id);
return view('product.camera.show', ['camera' => $camera]);
}
}
show
show.blade.php
resources/views/product/camera
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
55 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
<table>
<tr>
<td>Name</td>
<td>{{ $camera->name }}</td>
</tr>
<tr>
<td>Brand ID</td>
<td>{{ $camera->brand_id }}</td>
</tr>
<tr>
<td>Category ID</td>
<td>{{ $camera->category_id }}</td>
</tr>
<tr>
<td>Processor</td>
<td>{{ $camera->attributes['processor'] }}</td>
</tr>
<tr>
<td>Sensor Type</td>
<td>{{ $camera->attributes['sensor_type'] }}</td>
</tr>
<tr>
<td>Monitor Type</td>
<td>{{ $camera->attributes['monitor_type'] }}</td>
</tr>
<tr>
<td>Scanning System</td>
<td>{{ $camera->attributes['scanning_system'] }}</td>
</tr>
</table>
routes/web.php
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
56 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
// ...
Route::get(
'/product/camera/show/{id}',
[CameraController::class, 'show']
);
$ php artisan serve
id
localhost:8000/product/camera/show/11
id
store
show
edit
new.blade.php
show.blade.php
<tr>
<td><label for="attributes-processor">Processor</label></td>
<td><input id="attributes-processor" name="processor" type="text" value="{{ $camera->attributes
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
57 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
</tr>
id
public function search(Request $request){
$cameras = \App\Models\Product::where([
['attributes->processor', 'like', $request->processor],
['attributes->sensor_type', 'like', $request->sensor_type],
['attributes->monitor_type', 'like', $request->monitor_type],
['attributes->scanning_system', 'like', $request->scanning_system]
])->get();
return view('product.camera.search', ['cameras' => $cameras]);
}
product.camera.search
$cameras
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
58 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
where
delete
id
\App\Models\Product::where('id', $id)->delete();
where
delete
\App\Models\Product::where('attributes->sensor_type', 'CMOS')->delete();
}
sensor_type
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
59 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
60 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
62 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
63 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
How To Work with JSON in MySQL | DigitalOcean
64 of 64
https://www.digitalocean.com/community/tutorials/working-with-json-in-mysql
12/2/2021, 9:33 PM
Download