如何在 laravel 中将名称设置为 JSON 对象,因为我想在 Vue js 中为图片库显示插件。每当我传递数据时,我都会收到此错误:预期对象,得到数组。
{
"normal_size": [
{
"id": 1,
"image": "MICMIMC30651-6.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-6.jpg"
},
{
"id": 2,
"image": "MICMIMC30651-61.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-61.jpg"
},
{
"id": 3,
"image": "MICMIMC30651-62.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-62.jpg"
},
{
"id": 4,
"image": "MICMIMC30651-63.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-63.jpg"
}
]
}
我想要的输出:
image : {
"normal_size": [
{
"id": 1,
"image": "MICMIMC30651-6.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-6.jpg"
},
{
"id": 2,
"image": "MICMIMC30651-61.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-61.jpg"
},
{
"id": 3,
"image": "MICMIMC30651-62.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-62.jpg"
},
{
"id": 4,
"image": "MICMIMC30651-63.jpg",
"url": "http://localhost:8000/images/product/preview/MICMIMC30651-63.jpg"
}
]
}
`
Laravel 产品控制器代码将数据传递给 vue 组件
$normal_size = DB::table('products_images')->where([['products_id', '=', $product]])
->select('products_images.id','products_images.image')
->get()->ToArray();
foreach($normal_size as $value)
{
$value->url = 'http://localhost:8000/images/product/preview/'.$value->image;
}
$output = (array('normal_size' => $normal_size));
return response()->json(($output));
largeQ
函数式编程