【发布时间】:2018-06-30 16:03:28
【问题描述】:
我有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"
}
【问题讨论】:
-
dd($orders) 在前端控制器,结果如何?
-
似乎
$order->product_name是 json... -
@MahdiYounesi 已更新
-
@MilanChheda 是的,是我在问题中提到的 json。
-
因此您需要先使用
json_decode对其进行解码,然后才能在foreach中使用它
标签: php laravel laravel-blade