david001's avatar

How to unserialize data

I am developing an eCommerce application. I have store user order in Orders table where I have id/user_id/cart/create_at/updated_at field The data in the cart is serialized and as an admin I want to see which user_id or user has purchased what products. I want to show this on the view page.

My method
   public function userOrder(){
        $orders = Order::get();
        return $orders;
//return view('');
        
      }

[
{
id: 5,
user_id: 3,
cart: "O:8:"App\Cart":3:{s:5:"items";a:2:{i:5;a:5:{s:2:"id";i:5;s:4:"name";s:11:"DELL LAPTOP";s:5:"price";s:3:"799";s:3:"qty";i:1;s:5:"image";s:60:"public/product/iPhZNz1Fy9dYxAawNtVOspXvpTWvc28nFMwDTG7a.jpeg";}i:6;a:5:{s:2:"id";i:6;s:4:"name";s:15:"Lenovo ThinkPad";s:5:"price";s:3:"777";s:3:"qty";i:1;s:5:"image";s:60:"public/product/xa6HX9dvWxo1ZT0lGebO8PtWmg8NKEauvJHC4PjX.jpeg";}}s:8:"totalQty";i:2;s:10:"totalPrice";i:1576;}",
created_at: "2020-03-12 11:21:06",
updated_at: "2020-03-12 11:21:06"
},
{
id: 6,
user_id: 3,
cart: "O:8:"App\Cart":3:{s:5:"items";a:2:{i:5;a:5:{s:2:"id";i:5;s:4:"name";s:11:"DELL LAPTOP";s:5:"price";s:3:"799";s:3:"qty";i:2;s:5:"image";s:60:"public/product/iPhZNz1Fy9dYxAawNtVOspXvpTWvc28nFMwDTG7a.jpeg";}i:1;a:5:{s:2:"id";i:1;s:4:"name";s:11:"HP LAPTOPS ";s:5:"price";s:3:"877";s:3:"qty";i:1;s:5:"image";s:53:"product/yyO8bETsNup8nECU8sF0d7VBeRVbkEiQWthswpHP.jpeg";}}s:8:"totalQty";i:3;s:10:"totalPrice";i:2475;}",
created_at: "2020-03-13 09:16:58",
updated_at: "2020-03-13 09:16:58"
}
]

Please help me to display this data in view page.

0 likes
8 replies
pauljp's avatar

@david001 right.

  1. Customer Places Order

  2. Info is Stored In DB

  3. You fetch the values

  4. you return your view with variable

    return view('viewName')->with('varaibleName', $variable);

  5. on your view you can: {{ !! $varaibleName !!}

david001's avatar

@pauljp , I need to unserialize that data that is stored as serialize in cart filed on orders table. The way you mentioned is wrong, I have tired. Thanks

david001's avatar

Yes i have to unserialize , but i don't know how to do that?

pauljp's avatar

@david001 seems like an object , and cart seems to have been serialized. can you do a var_dump of it?

if it is an object.. you can do unserialize($object->cart);

Sinnbeck's avatar

Or try adding this to the order model

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

Please or to participate in this conversation.