dimanche 27 novembre 2016

Laravel Session cannot Increase the variable value

I need to increase a variable value through session every time i click on a button. But session always returns me the value to 1. Seems like it is forgetting the original value. If i use dd() method before retrieving the value it will be showing the value to be null. But after pushing the value to the session when i use dd() method then it will show the value is in the session.It seems like it is forgetting the value.Do i need to put web middlewware on laravel 5.3? or any other configurations to make it work? Please help me i tried to fix it in every possible way and i am stucked for about three days now. Thank you very much.Following is my code

my class

class Cart
{
public $totalQuantity;


public function __construct($oldVal)
{
    if ($oldVal) {

        $this->totalQuantity = $oldVal->totalQuantity;
    }

}

public function add()
{
    $this->totalQuantity=$this->totalQuantity+1;

}

}

my controller

public function getAddToCart(Request $request){

    $oldValue=0;
    // i get the value to null though i click button many times
    dd($request->session()->get('val'));

    if ($request->session()->has('val')) {
        $oldValue = $request->session()->get('val');
    }

    $cart = new Cart($oldValue);
    $cart->add();
    session(['val' => $cart]);
    // Here it shows the value.
    dd($request->session()->get('val'));

 }

My route

Route::get('/productInfo/{id}/addToCart',['uses'=>'searchController@getAddToCart','as'=>'product.addToCart'] );

2nd dd methods snip

please help me thanks a lot



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire