Laravel - 在会话中存储多个字段

时间:2016-12-21 13:49:39

标签: php laravel

目前,这只是在会话中存储first_name。我需要存储一些其他字段,这些字段位于同一个数据库行中,如user_role和city。我怎么能这样做?

DashboardContaroller.php

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

index.blade.php

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

查看

<p>{{Session::get('firstName','default value')}}</p>

3 个答案:

答案 0 :(得分:2)

您可以存储用户的对象,例如

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

答案 1 :(得分:1)

您可以将数据存储为数组:

Session::put('user', ['first_name' => $request->get('first_name'), 'user_role' => Auth::user()->user_role, 'city' => Auth::user()->city]);

查看:

<p>{{Session::get('user')['city']}}</p>

答案 2 :(得分:0)

这里你去

<强> DashboardContaroller.php

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

在你的视图中,迭代这样的数据:

@foreach (Session::get('user') as $user)
     {{$user}}
@endforeach

或使用

等特定键
{{Session::get('user')['first_name']}}