1 year ago

Getting info from a json_decode string

Posted 1 year ago by Nikki

I made a cart that when it's saved it is saved as a json_encode string. Everything looks great and kind of works. So what happens is that the cart is in a session then I save it to the database like this $order->cart = json_encode($cart). It saves nicely. The problem I have is that I can't access the product's title.

I can access for example the totalQty or the totalPrice by doing this {{ $cart->totalPrice }}, but not able to access anything else.

Here is the cart that is in my database

            "title":"Product 2",
            "description":"This is product 2",
            "created_at":"2018-08-23 12:19:44",
            "updated_at":"2019-02-11 09:17:14"}},

                "title": "Product 3",
                "description":"This is product 3",
                "created_at":"2018-08-23 12:22:18",
                "updated_at":"2019-02-11 09:17:38"


and this is how I'm decoding it

public function getInvoice($invoice_number)
    $orders = Order::where('invoice_number', $invoice_number)->get();

    foreach($orders as $order)
            $cart = json_decode($order->cart);
            $cart = [];

    return view('public.invoice', compact('cart'));

Please sign in or create an account to participate in this conversation.