2

I am trying to store the $user array in session but only id is stored in session. How can I put the first_name and last_name also ?

public function getIndex( Request $request )
{
    $this->data['firstNames'] = \DB::table('tb_users')->orderBy('first_name')->lists('first_name', 'id');
    $user = User::where('id', '=', $request->get('id'))->get()->toArray();
    Session::put('user', [ 'id' => $request->get('id'), 'last_name' => $request->get('last_name'), 'first_name' => $request->get('first_name'), ]);
    return view('dashboard.index',$this->data)->with('user', $user);
}

This is the select form

<form action="" method="post">
{!! Form::select('id', $firstNames) !!}
<button type="submit" value="Submit">Go</button>
</form>
3
  • stackoverflow.com/questions/37338526/… This Link May help you. Commented Dec 27, 2016 at 10:47
  • Let me try. Thank you. I keep you posted. Commented Dec 27, 2016 at 10:49
  • @Abu didn t worked for me. Commented Dec 27, 2016 at 11:44

3 Answers 3

1

To save array to session, use session() helper with this syntax:

session(['var' => $array]);

Later you can get array with session('var').

Also, it seems you're using wrong array structure. To use array in Form::select it should have this structure:

[1 => 'John', 2 => 'Dave']

https://laravel.com/docs/5.3/session#storing-data

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

Comments

0

To save multiple data into session use :

public function getIndex( Request $request )
{

        $this->data['firstNames'] = \DB::table('tb_users')->orderBy('first_name')->lists('first_name', 'id');
        $user = User::where('id', '=', $request->get('id'))->get()->toArray();
        Session::set('user', [ 'id' => $request->get('id'), 'last_name' => $request->get('last_name'), 'first_name' => $request->get('first_name'), ]);
        return view('dashboard.index',['user'=>Session::get('user')]);
    }

7 Comments

Hi @Rahul Getting syntax error, unexpected ''user)]);' (T_ENCAPSED_AND_WHITESPACE)
I tried. Now getting error Undefined variable: firstNames from index.blade.php because of the form <form action="" method="post"> {!! Form::select('id', $firstNames) !!} <button type="submit" value="Submit">Go</button> </form>
instead of $firstNames use $user->first_name
Getting this error now Trying to get property of non-object (View: /resources/views/dashboard/index.blade.php)
Sorry try $user['first_name'];
|
0
public function getAddToCart(Request $request,$id)
{   
    $product = Product::find($id);
    $oldCart = Session::has('cart') ? Session::get('cart') : null;
    $cart = new Cart($oldCart);
    $cart->add($product,$product->id);

    $request->session()->put('cart',$cart);
    return redirect()->route('product.index');
}

You may do like this. And this is my testing website http://lookoko.com ,when you click Add to Cart, the program will put the item array into session.All the codes you are able to locate on https://github.com/GoogleYY/shop-cart.git

1 Comment

And could you please help solve this problem I posted on stackoverflow.com/questions/41344126/…

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.