我有orders
表,我将订单数据保存在product_name
列中作为json,我可以在管理面板中获取订单数据但是当我尝试在前端使用相同的代码时也显示向用户订购详细信息,它会返回错误。
为foreach()提供的参数无效
public function index()
{
$orders = Order::orderby('id', 'desc')->paginate(10);
return view('admin.orders.index', compact('orders'));
}
admin index
@foreach($orders as $order)
{{$order->payment_id}}
{{$order->total_items()}}
@endforeach
admin edit page
(了解我如何从product_name
列获取详细信息)
public function edit($id, Request $request)
{
$order = Order::find($id);
$statuses = Orderstatus::all();
$addresses = Address::where('user_id', $order->user_id)->get();
return view('admin.orders.edit', compact('order', 'statuses', 'addresses', 'json'));
}
edit blade
<p>#{{$order->payment_id}}</p>
@foreach($order->product_name as $data)
{{ $data['name'] }}
{{$data['quantity']}}
{{number_format($order->total_price(), 0)}}
@endforeach
public function orders(Request $request)
{
$orders = Order::where('user_id', '=', Auth::user()->id)->orderby('id', 'desc')->paginate(5);
return view('front.orders', compact('orders'));
}
in blade
@foreach($orders as $order)
#{{$order->ordernu}}
@foreach($order->product_name as $data)
<li>{{ $data['name']}}</li>
@endforeach
@endforeach
然后我收到此错误:
为foreach()提供的参数无效
有什么想法吗?
dd($orders)
结果
LengthAwarePaginator {#700 ▼
#total: 2
#lastPage: 1
#items: Collection {#682 ▼
#items: array:2 [▼
0 => Order {#689 ▼
#fillable: array:10 [▶]
#casts: array:1 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:16 [▼
"id" => 15
"ordernu" => "4173870116"
"user_id" => 1
"orderstatus_id" => 7
"address_id" => 1
"payment_id" => null
"product_name" => ""[{\"id\":37,\"name\":\"test product\",\"price\":100000,\"quantity\":1,\"attributes\":[{\"attr\":{\"label\":\"Gray\",\"price\":\"7000.00\"}}],\"conditions\":[]} ▶"
"quantity" => null
"price" => null
"note" => "hello this is for test"
"address" => null
"phone" => "xxxxxxx"
"buyer_name" => "xxxxxx"
"buyer_email" => "admin@admin.com"
"created_at" => "2018-01-22 10:38:53"
"updated_at" => "2018-01-22 10:38:53"
]
#original: array:16 [▶]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => Order {#691 ▶}
]
}
#perPage: 5
#currentPage: 1
#path: "http://domail.dev/orders"
#query: []
#fragment: null
#pageName: "page"
}
答案 0 :(得分:0)
要获得所需的结果,您需要遍历orders.items
,因为它是一个分页器对象
foreach($orders.items as $order)
{
//rest of code
}