Php Json数据在laravel中一起返回
我有一个包含以下数据的json列:Php Json数据在laravel中一起返回,php,json,laravel,laravel-blade,Php,Json,Laravel,Laravel Blade,我有一个包含以下数据的json列: { "address":"testing address, phone number, country, state, name of the person etc.", "post_method":"tiki", "post_price":"15000", "weight":"2" } 现在,当我尝试在视图中返回此数据时,如下所示: @foreach($order->shipment_data as $shipment) //ship
{
"address":"testing address, phone number, country, state, name of the person etc.",
"post_method":"tiki",
"post_price":"15000",
"weight":"2"
}
现在,当我尝试在视图中返回此数据时,如下所示:
@foreach($order->shipment_data as $shipment) //shipment_data is the name of json column which holds this data
{{$shipment}}
@endforeach
它返回address、post_方法、post_价格和权重下的所有数据,就像一个段落一样
我无法单独获得每个零件,如:
{{$shipment['post_price']}}
它返回:
非法字符串偏移量“后价格”视图:
有什么想法吗
使现代化
{{dd$order->shipping_data}的结果
因为您已经将其强制转换为数组,所以在您的例子中,$order->shipping\u数据只是一个数组。您可以按如下方式访问单个列:
$order->shipment_data['post_price']
没有必要使用foreach 您需要将json转换为数组。您可以使用json_解码函数来实现这一点。另外,若您使用新版本的Laravel,您可以通过添加
protected $casts = [
'shipment_data' => 'array',
];
这应该是可行的。在发布之前先添加索引0_price@aldrin27非法字符串偏移量您需要json_解码数据吗?正如@OluwatobiSamuelOmisakin建议先解码您的json。@OluwatobiSamuelOmisakin我不这么认为,因为以前我用json_encode存储数据,它在每个部分之前添加\然后我删除了编码代码所以现在它实际上只是保存了一个数组{我不认为我需要解码,只要我不编码它们,它说试图获取非对象的属性“post_price”,你确实需要使用json_decode。请参阅更新的答案。当我按照你的方式@foreachjson_decode$order->shipping_data as$shipping时,它说json_decode希望参数1是字符串,数组givencould$order->shipping_data和添加到你的问题?更新了我的答案。你只需要{{$order->shipping\u data->post\u price}而不需要循环
protected $casts = [
'shipment_data' => 'array',
];