2

I need to set an session with multiple variables and then get the session back in the next page.

here what I try

in controller

public function postInvoice(Request $request)
    {
        $name = $request->name;
        $email = $request->email;
        $phone = $request->phone;
        $zip = $request->zip;
        $country = $request->country;
        $street_address = $request->street_address;
        $city = $request->city;
        $state = $request->state;

        Session::flash('userInfo', [$name, $email, $phone, $zip, $country, $street_address, $city, $state]);
        return view('portal.cart.standardPayment');
    } 

and then in my blade page when I try to get the session back I try

{!! Session::get('userInfo', $name) !!}

but got

Undefined variable: name

How can I get the session variables from the array?

2 Answers 2

1

You are passing array of values, so try Session::get('userInfo')[0]. The get function second parameter is the default value

Sign up to request clarification or add additional context in comments.

1 Comment

Nice idea so if I need to add something like Session::get('userInfo')[name] I need to change the Session::flash to be something like Session::flash('userInfo', ['name'=>'$name'] yes??
1

I know this answer is late, but it may help someone.

If you want to store an array of data in session, the easiest way to do it is:

  1. using session()->push() method
session()->push('user.first_name', 'John');
  1. using session()->put('array_name', [])
session()->put('names', ['john', 'doe']);

Laravel Sessions Documentation

2 Comments

Aha it's not just late answer it's also different version of Laravel I was using Laravel 5.2 at that time anyway thanks dear I think it will help others who using newest Laravel version.
Its my pleasure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.