Laravel 我的订单表中有一个cart对象需要显示项目

Laravel 我的订单表中有一个cart对象需要显示项目,laravel,laravel-5,laravel-blade,Laravel,Laravel 5,Laravel Blade,您好,我的订单表字段中有一个cart对象,因此我需要显示订单项目,但我无法确定如何显示项目,以将对象取消序列化为数组或其他内容 我的目标是 "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:"\x

您好,我的订单表字段中有一个cart对象,因此我需要显示订单项目,但我无法确定如何显示项目,以将对象取消序列化为数组或其他内容

我的目标是

"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;} ◀"

您可以通过使用模型来实现这一点

这将自动对数组中指定的字段进行序列化/取消序列化

在您的情况下,将此添加到
订单
模型:

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

显示生成此类对象的代码$order=new order()$订单->购物车=序列化($cart)$订单->fname=$request->input('fname')$订单->lname=$request->input('lname')$订单->地址1=$request->input('address1')$订单->地址2=$request->input('address2')$订单->zip=$request->input('zip')$订单->状态=$request->input('state')$订单->城市=$request->输入('city')$订单->付款\u id=$charge->id@RahulPawar我只是将$cart对象保存在db with order中,我需要在我的单订单页面上显示订单项,以便我的控制器现在有此代码,请提供建议<代码>公共函数getordersingle($id){$order=order::find($id);返回视图('backend/orders/single')->withOrder($order);}因此订单变量我有所有详细信息,但我不知道如何在我的单订单视图中循环使用变量$order->cart中的项目。我认为第一个解决方案是不保存整个购物车模型,只保存所需内容。这将使结构更加清晰。如果使用强制转换,则不需要
serialize()
此数据。但在中删除“从我的购物车中序列化”变量时不保存此数据。在保存到购物车时,我建议实际传递所需的内容,例如购物车项目数组,而不是
购物车
模型本身。
protected $casts = [
    'cart' => 'array',
];