0

I want to create session based on the $id (unique id). This is for so that when each user enters the web app they will have their own session id and also their own array. But now when i put it in the function and every time i enter the function the session is not being stored and it is overriding the previous session. so how do i modify the code so that all the session will be created for each user in function test().

Here is my code:

public function test()
{
    $id = 'new3'; //later on will be auto generate id
    $array[$id] = ['one' => 'abc', 'two' => 'def'];

    if (!session($id)) {
        session($array);
    }

    dd(session()->all());
}
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90

5 Answers5

1
 Session::put('variable', $yourvalue);

Then to get the session:

 Session::get('variable');
Raf
  • 9,681
  • 1
  • 29
  • 41
Kuldeep Mishra
  • 3,846
  • 1
  • 21
  • 26
0

You should manipulate laravel session like this:

public function test(Request $request)
{
    $id = 'new3'; //later on will be auto generate id
    $array[$id] = ['one' => 'abc', 'two' => 'def'];

// Determining if an item exists in the session.
if (!$request->session()->has($id)) {
    // Storing data in the session.
    $request->session()->put($id, $array[$id]);
}

    dd($request->session()->all());
}
Cong Chen
  • 2,436
  • 12
  • 21
  • but i just tried the session. I gave it first the id new below is the array "new" => array:2 [▼ "one" => "abc1" "two" => "def" ] but when i change the id to new1 the above array and session is no longer available and it will be replace with the below "aaaa" => array:2 [▼ "one" => "abc1" "two" => "def" ] the id will change from new to aaaa – RollCasual Lee Aug 21 '17 at 06:24
  • ya i used your code and run its still the same. the session is not being stored and it is creating new session everytime the page loads to the same function text() – RollCasual Lee Aug 21 '17 at 06:36
0

The correct syntax for this is...

Session::set('variableName', $value);

To get the variable, you'd use...

Session::get('variableName');

If you need to set it once, I'd figure out when exactly you want it set and use Events to do it. For example, if you want to set it when someone logs in, you'd use...

Event::listen('auth.login', function()
{
    Session::set('variableName', $value);
});
Sushil
  • 2,324
  • 1
  • 27
  • 26
0

I think you need try with this:

public function test(Request $request)
{
    $id = 'new3'; //later on will be auto generate id
    $array[$id] = ['one' => 'abc', 'two' => 'def'];

// Determining if an item exists in the session.
if (!$request->session()->has($id)) {
  // Storing data in the session.
  $request->session()->put($id, $array[$id]);
} 

    dd($request->session()->all());
    if($request->session()->has($id)) {
            $request->session()->forget($id);
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
0

Even I had little trouble manipulating arrays stored in session laravel. After trial and error, I was able to make things work , like this:

    $data['quantity'] = $request['quantity'];
    $data['price'] = $price;
    $data['tax'] = $tax;
    $data['shipping'] = 0;


    if($request->session()->has('cart')){
        $cart = $request->session()->get('cart', collect([]));
        $is_id_duplicate = false;
        foreach($cart as $key=> $c){
            if ($c['id']== $request->id) {
               $is_id_duplicate=true;
            break;
            }
        }

        if ($is_id_duplicate) {
             $cart = $cart->map(function ($object, $key) use ($request) {
                if($object['id'] == $request->id){
                    $object['quantity'] = $request->quantity;
                }
                return $object;
            });
            $request->session()->put('cart', $cart);


        }else{
             $cart->push($data);
        }

        echo "<br><pre>";
        print_r($cart);
        echo "</pre>";


    }
    else{
        $cart = collect([$data]);
        $request->session()->put('cart', $cart);
    }
amar deep
  • 111
  • 2
  • 15