maf*_*tis 3 php serialization laravel
我orders使用该serialize方法将我的购物车数据保存到表中,现在在我的订单“查看”页面中,我想向用户显示它们以显示他们的订单历史记录。
如何将以前序列化的数据恢复为 PHP 中的可用对象/数组?
我保存数据的代码片段:$order->cart = serialize($cartItems);.
我尝试返回我的订单索引视图的方法:
/**
* Action to receive all orders from the current
* logged-in user. This action will return the
* 'front.orders' view with the orders compacted inside.
*
* @return orders view
*/
public function orders() {
// get the orders from the current logged in user
$orders = Order::where('user_id', '=', Auth::user()->id)->get();
// view the `front.orders` page passing in the `orders` variable
return view('front.orders', compact('orders'));
}
Run Code Online (Sandbox Code Playgroud)
$orders = $orders->map(function($i) {
$i->cart = unserialize($i->cart);
return $i;
});
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用访问器自动反序列化属性:
public function getCartAttribute($value)
{
return unserialize($value);
}
Run Code Online (Sandbox Code Playgroud)
或者只是在 Blade 中反序列化数据:
@foreach ($orders as $order)
{{ unserialize($order->cart)->someData }}
@endforeach
Run Code Online (Sandbox Code Playgroud)