我的订单表中有一个购物车对象,需要显示商品

时间:2018-09-21 10:28:37

标签: laravel laravel-5 laravel-blade

您好,我的订单表字段中有一个购物车对象,因此我需要显示订单商品,但是我无法弄清楚如何显示商品以将商品反序列化为数组或其他东西。

我的对象就像

"O:8:"App\Cart":3:{s:5:"items";a:1:{i:4;a:3:{s:3:"qty";i:1;s:5:"price";i:44;s:4:"item";O:11:"App\Product":26:{s:13:"\x00*\x00connection";s:5:"mysql";s:8:"\x00*\x00table";N;s:13:"\x00*\x00primaryKey";s:2:"id";s:10:"\x00*\x00keyType";s:3:"int";s:12:"incrementing";b:1;s:7:"\x00*\x00with";a:0:{}s:12:"\x00*\x00withCount";a:0:{}s:10:"\x00*\x00perPage";i:15;s:6:"exists";b:1;s:18:"wasRecentlyCreated";b:0;s:13:"\x00*\x00attributes";a:20:{s:2:"id";i:4;s:8:"prodname";s:7:"GE GSWF";s:15:"proddescription";s:303:"<p><span style="color:#212529;font-family:Montserrat, sans-serif;font-size:13px;font-weight:600;text-align:center;">Lorem ipsum is a dummy text </span><span style="color:#212529;font-family:Montserrat, sans-serif;font-size:13px;font-weight:600;text-align:center;">Lorem ipsum is a dummy text</span></p>";s:4:"slug";s:9:"ge_gswf14";s:11:"gall_images";N;s:13:"feature_image";s:14:"1536994278.png";s:8:"brand_id";i:2;s:11:"category_id";i:2;s:5:"price";i:44;s:12:"manufacturer";s:10:"SmartWater";s:8:"features";s:123:"Delivers clean, clear water that looks and tastes great, Reduces sediment, lead, cysts, and turbidity in your water and ice";s:6:"height";s:8:"10 Meter";s:5:"width";s:5:"Meter";s:6:"length";s:7:"4 Meter";s:10:"itemweight";s:6:"10 lbs";s:10:"shipweight";s:6:"12 lbs";s:10:"fridgecomp";s:62:"Fridge Compatibility Fridge Compatibility Fridge Compatibility";s:6:"status";s:7:"publish";s:10:"created_at";s:19:"2018-09-15 06:51:18";s:10:"updated_at";s:19:"2018-09-15 06:51:18";}s:11:"\x00*\x00original";a:20:{s:2:"id";i:4;s:8:"prodname";s:7:"GE GSWF";s:15:"proddescription";s:303:"<p><span style="color:#212529;font-family:Montserrat, sans-serif;font-size:13px;font-weight:600;text-align:center;">Lorem ipsum is a dummy text </span><span style="color:#212529;font-family:Montserrat, sans-serif;font-size:13px;font-weight:600;text-align:center;">Lorem ipsum is a dummy text</span></p>";s:4:"slug";s:9:"ge_gswf14";s:11:"gall_images";N;s:13:"feature_image";s:14:"1536994278.png";s:8:"brand_id";i:2;s:11:"category_id";i:2;s:5:"price";i:44;s:12:"manufacturer";s:10:"SmartWater";s:8:"features";s:123:"Delivers clean, clear water that looks and tastes great, Reduces sediment, lead, cysts, and turbidity in your water and ice";s:6:"height";s:8:"10 Meter";s:5:"width";s:5:"Meter";s:6:"length";s:7:"4 Meter";s:10:"itemweight";s:6:"10 lbs";s:10:"shipweight";s:6:"12 lbs";s:10:"fridgecomp";s:62:"Fridge Compatibility Fridge Compatibility Fridge Compatibility";s:6:"status";s:7:"publish";s:10:"created_at";s:19:"2018-09-15 06:51:18";s:10:"updated_at";s:19:"2018-09-15 06:51:18";}s:10:"\x00*\x00changes";a:0:{}s:8:"\x00*\x00casts";a:0:{}s:8:"\x00*\x00dates";a:0:{}s:13:"\x00*\x00dateFormat";N;s:10:"\x00*\x00appends";a:0:{}s:19:"\x00*\x00dispatchesEvents";a:0:{}s:14:"\x00*\x00observables";a:0:{}s:12:"\x00*\x00relations";a:0:{}s:10:"\x00*\x00touches";a:0:{}s:10:"timestamps";b:1;s:9:"\x00*\x00hidden";a:0:{}s:10:"\x00*\x00visible";a:0:{}s:11:"\x00*\x00fillable";a:0:{}s:10:"\x00*\x00guarded";a:1:{i:0;s:1:"*";}}}}s:8:"totalQty";i:1;s:10:"totalPrice";i:44;} ◀"

然后我将以下对象保存在我的订单表中

        $order = new Order();
        $order->cart = serialize($cart);
        $order->fname = $request->input('fname');
        $order->lname = $request->input('lname');
        $order->address1 = $request->input('address1');
        $order->address2 = $request->input('address2');
        $order->zip = $request->input('zip');
        $order->state = $request->input('state');
        $order->city = $request->input('city');
        $order->payment_id = $charge->id;

1 个答案:

答案 0 :(得分:1)

您可以通过使用模型casts来实现。

这将自动序列化/反序列化您在数组中指定的字段。

根据您的情况,将其添加到Order模型中:

protected $casts = [
    'cart' => 'array',
];