Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Laravel Store Array in Session

I have been having trouble storing an array in session. I am making a shopping cart and it doesn't seem to work.

public function __construct(){

  $product = array(1,2,3,4);
  Session::push('cart', $product);


and then retrieve it in the view like this.


However I keep getting an error like this.

htmlentities() expects parameter 1 to be string, array given

Any clues and advice on how to create a shopping cart that stores an array of items.

like image 368
Nello Avatar asked May 20 '16 05:05


People also ask

How do I push an array to an existing session in laravel?

session()->push('products.name', $name); session()->push('products. class', $class);

Where session store in Laravel?

The session configuration is stored in config/session. php . Be sure to review the well documented options available to you in this file. By default, Laravel is configured to use the file session driver, which will work well for the majority of applications.

What is session data in Laravel?

Sessions are used to store information about the user across the requests. Laravel provides various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data.

Where is user information stored in Laravel?

That user information is typically placed in a persistent store / backend that can be accessed from subsequent requests. Laravel ships with a variety of session backends that are accessed through an expressive, unified API.

What is the use of open method in Laravel?

- The `open` method would typically be used in file based session store systems. Since Laravel ships with a `file` session driver, you will almost never need to put anything in this method. You can leave it as an empty stub. It is simply a fact of poor interface design (which we'll discuss later) that PHP requires us to implement this method.

Which session driver should I use for my Laravel application?

Be sure to review the options available to you in this file. By default, Laravel is configured to use the file session driver, which will work well for many applications. If your application will be load balanced across multiple web servers, you should choose a centralized store that all servers can access, such as Redis or a database.

Video Answer

4 Answers

If you need to use the array from session as a string, you need to use Collection like this:

$product = collect([1,2,3,4]);
Session::push('cart', $product);

This will make it work when you will be using {{Session::get('cart');}} in your htmls. Be aware of Session::push because it will append always the new products in sessions. You should be using Session::put to be sure the products will be always updating.

like image 186
mvpasarel Avatar answered Sep 25 '22 01:09


You're storing an array in the session, and since {{ }} expects a string, you can't use {{Session::get('cart')}} to display the value.

The {{ $var }} is the same as writing echo htmlentities($var) (a very simple example).

Instead, you could do something like:

@foreach (Session::get('cart') as $product_id)
like image 45
Kirk Beard Avatar answered Sep 26 '22 01:09

Kirk Beard

If you use 'push', when initially creating the array in the session, then the array will look like this:

    0 => [1,2,3,4]

Instead you should use 'put':

$products = [1,2,3,4];
$request->session()->put('cart', $products);

Any subsequent values should be pushed onto the session array:

$request->session()->push('cart', 5);
like image 32
omarjebari Avatar answered Sep 22 '22 01:09


You can use .:

$product = array(1,2,3,4);
like image 28
vinh hoang Avatar answered Sep 22 '22 01:09

vinh hoang