gpt4 book ai didi

php - 在 laravel 中反序列化数据

转载 作者:行者123 更新时间:2023-12-03 18:45:39 25 4
gpt4 key购买 nike

我使用 serialize 方法将我的购物车数据保存到 orders 表中,现在在我的订单“查看”页面中,我想将它们显示给用户以显示他们的订单历史。

如何在 PHP 中将之前序列化的数据恢复为可用的对象/数组?

我保存数据的代码片段:$order->cart = serialize($cartItems);

我尝试返回我的订单索引 View 的方法:

/**
* 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'));
}

最佳答案

您可以使用 map() unserialize 的方法整个集合的购物车属性:

$orders = $orders->map(function($i) {
$i->cart = unserialize($i->cart);
return $i;
});

或者,您可以使用 accessor自动反序列化属性:

public function getCartAttribute($value)
{
return unserialize($value);
}

或者只是 unserialize Blade 中的数据:

@foreach ($orders as $order)
{{ unserialize($order->cart)->someData }}
@endforeach

关于php - 在 laravel 中反序列化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48125903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com