简体   繁体   English

Laravel-如何向会话添加数据

[英]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. 嗨,我想在会话中存储以下选择,并在其他xxxx.blade.php页面中回显。 Whenever I select new one the previous selection needs to be dropped. 每当我选择新的时,都需要删除先前的选择。

DashboardController.php DashboardController.php

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

index.blade.php 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('/');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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