在store
函数中,我想将JSON数据保存在数据库中而无需逃脱URL,例如:
$arr = [
'images'=>['original'=>'/uploads/contents/' . basename($filename)],
'thumbnail'=>'/uploads/contents/' . basename($filename)
];
$data = Contents::create([
'title' => $request->title,
'description' => $request->description,
'featured_images' => $arr,
'visit' => 0,
]);
将此结果存储为$arr
在数据库中我有:
{
"images":
{"original":"/uploads/contents/636459965493789558.jpg"},
"thumbnail":"/uploads/contents/636459965493789558.jpg"
}
在那,URL逃脱了。我该如何存储它而不逃脱URL?
我的模型选项:
protected $casts=[
'featured_images'=>'array'
];
您可以手动编码JSON,以防止这种方式
json_encode($value, JSON_UNESCAPED_SLASHES);
我认为您应该这样留下。只要您想使用它,只需这样的 json_decode
。
<?php
$a = json_decode('{
"images":
{"original":"/uploads/contents/636459965493789558.jpg"},
"thumbnail":"/uploads/contents/636459965493789558.jpg"
}', true);
echo $a["images"]["original"];
?>
结果:
/uploads/contents/636459965493789558.jpg