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>
Selim
  • 723
  • 2
  • 11
  • 17

3 Answers3

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

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
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')]);
    }
Rahul
  • 2,374
  • 2
  • 9
  • 17
  • Hi @Rahul Getting `syntax error, unexpected ''user)]);' (T_ENCAPSED_AND_WHITESPACE)` – Selim Dec 27 '16 at 11:08
  • I tried. Now getting error **Undefined variable: firstNames** from index.blade.php because of the form `
    {!! Form::select('id', $firstNames) !!}
    `
    – Selim Dec 27 '16 at 11:39
  • instead of `$firstNames` use `$user->first_name` – Rahul Dec 27 '16 at 11:44
  • Getting this error now `Trying to get property of non-object (View: /resources/views/dashboard/index.blade.php)` – Selim Dec 27 '16 at 11:47
  • Sorry try `$user['first_name'];` – Rahul Dec 27 '16 at 11:58
  • Getting this `Invalid argument supplied for foreach() (View:/resources/views/dashboard/index.blade.php)` – Selim Dec 27 '16 at 12:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131585/discussion-between-rahul-and-selim). – Rahul Dec 27 '16 at 12:17
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

Mark
  • 13
  • 3
  • And could you please help solve this problem I posted on http://stackoverflow.com/questions/41344126/looking-for-some-helps-in-adding-policy-to-my-laravel-projects – Mark Dec 27 '16 at 11:48