why session push in laravel store my value in new array with zero index

9.9k views Asked by At

my first code line is

Session::put('step_1', array('security' => 'yes'));

2nd is

    $vat=10;
    \Session::push('step_1.vat',$vat);

my current output:

Array
(
    [security] => yes
    [vat] => Array
        (
            [0] => 10
        )

)

My Desired Output:

Array
(
    [security] => yes
    [vat] => 10
)

hot to achieve my desired output, please suggest, thanks.

3

There are 3 answers

0
chanafdo On BEST ANSWER

You could simply use

Session::put('step_1.security', 'yes'));
$vat=10;
Session::put('step_1.vat', $vat);
2
Rooshan Akthar On

Yes, I have been into the mess. What I did was, getting the session to a variable, forgetting it, adding more data and adding to the session variable again. This is a simple workaround in your case,

$session = Session::get("step_1");
Session::forget("step_1");
$session['vat'] = 10;
Session::put('step_1', $session);

if you dig into the laravel source, Session::push,

/**
 * Push a value onto a session array.
 *
 * @param  string  $key
 * @param  mixed   $value
 * @return void
 */
public function push($key, $value)
{
    $array = $this->get($key, array());

    $array[] = $value;

    $this->put($key, $array);
}

This is the reason for adding an index.

0
Muhammad On

i found this way very easy and convenient:

$step_1 = \Session::get('step_1');
$step_1['vat'] = $vat;
\Session::put('step_1', $step_1);