Change value in session and save it. Laravel

I want to update product price when I submit coupon code I named my coupon as ticket. This is my function:

public function order(Request $request)
    {
        $products = session('cart');

        $ticket_code = Input::get('ticket');
        $ticket = Ticket::where('ticket', $ticket_code)->first();

        foreach($products as $p){
            $price = $p['price'];
        }

        if(count($products) && $ticket['max'] > $ticket['used']) {
            $subtotal = [$price*70/100];
            $ticket->used += 1;
            $ticket->save();
            foreach($products as $p){
                $p['price'] = $subtotal;
                $p->save();
            }
            flash()->success('Kuponas sėkmingai panaudotas!');
            return view('cart.order')->with(array(
                'products' => $products,
                'subtotal' => $subtotal,
            ));

        }

        else {
            $finalTotal = 0;
            $subtotal = [];
            return view('cart.order')->with(array(
                'products' => $products,
                'subtotal' => $subtotal,
                'finalTotal' => $finalTotal
            ));
        }


    }

this code doesnt work:

  foreach($products as $p){
                $p['price'] = $subtotal;
                $p->save();
            }

I get error: Call to a member function save() on array

Without the foreach the ticket itselfs works. It changes the price when submited, but only in that one page. Then when I go to payment site, the price is still displayed without ticket used.

THE WORKING SOLUTION

    foreach($products as &$item) {
                    $item['price'] = $item['price']*70;
                    $item['price'] = $item['price'] / 100;
                }

Session::put('cart', $products);

Upvotes: 2

Views: 4009

Answers (1)

THIS WORKED

foreach($products as &$item) {
                    $item['price'] = $item['price']*70;
                    $item['price'] = $item['price'] / 100;
                }

Session::put('cart', $products);

Upvotes: 2

Related Questions