简体   繁体   中英

how to store array of data in Session and retrieve it Laravel?

I was trying to store some data in session through ajax. Means from Ajax request i would get the product_id and it put it inside product array through Session. And I want to retrieve every session array data.

Every Ajax request product_id will be stored into Session Array: // for storing $products into session following controller .But it stored only last one not the previous request

public function postEnquote(Request $request)
    {
       $product = Product::where('id',$request->Input(['product_id']))->first();
       Session::put('product', $product);
    }

// For retrieving every session data i used following one, but doesn't work properly means i couldn't get every session data..

  public function enquoteList()
    {
      foreach(Session::get('product') as $test)
      {
         var_dump($test->id);
      }

     }

You will have to use push here.

Session::push('product', $product);

So all new $product push into 'product' session variable.

Use push to add a value to an array

public function postEnquote(Request $request)
{
   $product = Product::where('id',$request->Input(['product_id']))->first();
   Session::push('product', $product);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM