简体   繁体   中英

Laravel - How to add data to session

Hi I would like to store the following selection in session and echo in an other xxxx.blade.php page. Whenever I select new one the previous selection needs to be dropped.

DashboardController.php

$this->data['companyNames'] = \DB::table('tb_users')->orderBy('company_name')->lists('company_name', 'id');

index.blade.php

<form action="impersonate" method="post">
{!! Form::select('id', $companyNames) !!}
<button type="submit" value="Submit">Go</button>
</form>

All you have to do to switch users it to login a different one:

Auth::login($user);

This is it made very simply:

Create a form with inputs:

<form action="/impersonate" method="post">
    {{ csrf_field() }}

    <select name="user_id">
      <option value="1">User 1</option>
      <option value="2">User 2</option>
      <option value="3">User 3</option>
      <option value="4">User 4</option>
    </select>

    <your submit button>
</form>

And a route to login your user:

Route::post('impersonate', function() {
    $user = User::find(request()->get('user_id'));

    Auth::login($user_id);

    return Redirect::to('/');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM