Json I';在将数据传递到前端时,我遇到了一些错误
在将数据传递到前端时,我遇到了一些错误,但是当我返回这个$order时,它会显示如下JSON数据Json I';在将数据传递到前端时,我遇到了一些错误,json,laravel,Json,Laravel,在将数据传递到前端时,我遇到了一些错误,但是当我返回这个$order时,它会显示如下JSON数据 [ { "id": 1, "customer_id": 3446467182106354, "products": { "1": { "'name'": "Soap", "'quantity'&quo
[
{
"id": 1,
"customer_id": 3446467182106354,
"products": {
"1": {
"'name'": "Soap",
"'quantity'": "1"
},
"2": {
"'name'": "Shampoo",
"'quantity'": "1"
}
},
"total_amount": 798,
"status": "pending",
"created_at": "2020-10-21T08:51:15.000000Z",
"updated_at": "2020-10-21T08:51:15.000000Z"
}
]
Property [products] does not exist on this collection instance. (View: C:\xampp\htdocs\blog\resources\views\customer\orders.blade.php)
但是当我把它传给前端时,它是这样说的
[
{
"id": 1,
"customer_id": 3446467182106354,
"products": {
"1": {
"'name'": "Soap",
"'quantity'": "1"
},
"2": {
"'name'": "Shampoo",
"'quantity'": "1"
}
},
"total_amount": 798,
"status": "pending",
"created_at": "2020-10-21T08:51:15.000000Z",
"updated_at": "2020-10-21T08:51:15.000000Z"
}
]
Property [products] does not exist on this collection instance. (View: C:\xampp\htdocs\blog\resources\views\customer\orders.blade.php)
顺便说一下,我的数据库中有一个json数据在使用此数据之前,您应该使用
json\u decode()
函数您需要循环,您无法打印collection,因此您需要像这样更新控制器和刀片文件
[
{
"id": 1,
"customer_id": 3446467182106354,
"products": {
"1": {
"'name'": "Soap",
"'quantity'": "1"
},
"2": {
"'name'": "Shampoo",
"'quantity'": "1"
}
},
"total_amount": 798,
"status": "pending",
"created_at": "2020-10-21T08:51:15.000000Z",
"updated_at": "2020-10-21T08:51:15.000000Z"
}
]
Property [products] does not exist on this collection instance. (View: C:\xampp\htdocs\blog\resources\views\customer\orders.blade.php)
控制器
公共功能客户订单($id)
{
$orders=Order::where('customer_id',$id)->get();
返回视图('customer.orders',compact('orders');
}
刀片
产品
全部的
地位
@foreach($orders作为$order)
@foreach($order->products as$product)
{{$product['name']}
@endforeach
{{$order->total}
{{$order->status}
@endforeach
这样,您将获得所有数据,查看您在问题中提到的json数据,Products是Products的数组,它位于主数组中 您可以通过这种方式访问产品 为产品制作另一个foreach循环
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Products</th>
<th scope="col">Total</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
@foreach ($orders as $order)
<tr>
@foreach{{$order->products as $product}}
<th>{{ $product->name }}</th>
@endforeach
<td>{{ $order->total }}</td>
<td>{{ $order->status }}</td>
</tr>
@endforeach
</tbody>
产品
全部的
地位
@foreach($orders作为$order)
@foreach{{$order->products as$product}
{{$product->name}
@endforeach
{{$order->total}
{{$order->status}
@endforeach
欢迎来到So。。在使用products
这里是我的刀片代码产品总状态{{{$order->products}{{{$order->Total}}{{$order->Status}}公共函数customerOrders($id)的地方添加代码{$order=order::where('customer_id',$id)->get();return view('customer.orders',compact('order');}请不要在注释中对此提出疑问,我将在何处使用此json_decode()函数刀片或控制器???在何处返回此数据?现在显示此错误ErrorException htmlspecialchars()参数1应为字符串,数组给定(视图:C:\xampp\htdocs\blog\resources\views\customer\orders.blade.php)是的,因为再次$order->products
这是要显示的产品的集合。?我现在更新了代码检查我也根据您的指示更新了代码,但现在它显示了ErrorException未定义的偏移量:1是的,这是链接:模型之间没有关系刚刚将JSON数据保存在DBErrorException未定义的偏移量:1未定义哪种收款的抵销?订单还是产品?