I have a JSON array in an array:
{
layouts: [
{
w: 6,
h: 4,
x: 0,
y: 0,
i: "n0",
minW: 1.5,
minH: 1,
maxH: 1000,
moved: false,
static: false,
widget: "Clock"
},
{
w: 2,
h: 2,
x: 0,
y: 4,
i: "n1",
minW: 1,
minH: 1,
maxH: 1000,
moved: false,
static: false,
widget: "Weather"
}
]
}
I need to store each widget layout in a database. When I'm trying to execute the query It returns an error that the values are empty. I tried some examples from here but they don't work for me.
function:
public function store(Request $request)
{
$JSON = json_decode($request);
foreach (array($JSON) as $data) {
$i = 0;
$w = $data[$i]['w'];
$h = $data[$i]['h'];
$x = $data[$i]['x'];
$y = $data[$i]['y'];
$i = $data[$i]['i'];
$minW = $data[$i]['minW'];
$minH = $data[$i]['minH'];
$maxH = $data[$i]['maxH'];
$moved = $data[$i]['moved'];
$static = $data[$i]['static'];
$type = $data[$i]['type'];
DB::table('widgets')->insert(
['w' => $w,
'h' => $h,
'x' => $x,
'y' => $y,
'i' => $i,
'minW' => $minW,
'minH' => $minH,
'maxH' => $maxH,
'moved' => $moved,
'static' => $static,
'type_widget' => $type]
);
$i++;
}
return response()->json(201);
}
Could anyone tell me what is the correct way to loop through the data to save it?