Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
Storing associative array in session variable laravel

Actually, I am creating a shopping cart. I need to store the product_id and the quantity in the session, so that I can retrieve these into the Cart page.

Currently, I am adding values to the session variable using the push method-

$request->session()->push('order.products', $request->product_id);

It stores data in this structure-

array (size=1)
      'products' => 
        array (size=2)
          0 => string '11' (length=2)
          1 => string '9' (length=1)

But, in this way I am not able to store the quantity. I think storing associative array into the session can solve my problem.

So, how can I do that??

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Please try it.

// store data in session.

   $request->session()->push('productsid', ['order.products'=>$request->product_id]);

// then get session data here.
$array = Session::get('productsid');
over 4 years ago · Santiago Trujillo Denunciar

0

The Laravel didnt provided this feature but you can achieve what you want within a function:

/**
 * this method will append new values to session stored array,
 *
 * This method functionality has advantage over Laravel's session()->push is:
 * it can accept an associate array as well as normal array
 *
 * @param  string $session_key    the session key which associated with The 'Array'
 * @param  array $appendingArray The array which will be append to existing session-stored array
 * @return void
 */
private static function session_append($session_key, $appendingArray)
{
    session()->put($session_key, array_merge(
        session()->get($session_key),
        $appendingArray
    ));
}

and as long as it depends on session() helper, it can be a global helper as well.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda