Laravel - 在会话中存储多个字段
Currently this is storing only first_name in session. I need to store some other fields which are in the same database row like user_role and city. How can I do that ?
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>
View
<p>{{Session::get('firstName','default value')}}</p>
目前,这只是在会话中存储first_name。 我需要存储一些其他字段,这些字段位于同一个数据库行中,如user_role和city。 我该怎么办? p>
DashboardContaroller.php strong> p>
index.blade.php strong > p>
查看 strong> p>
public function getIndex(Request $ request)\ n {
$ this-&gt; data ['firstNames'] = \ DB :: table('tb_users') - &gt; orderBy('first_name') - &gt; lists('first_name','first_name'); \ n Session :: put('firstName',$ request-&gt; get('first_name'));
返回视图('dashboard.index',$ this-&gt;数据);
}
code> pre>
&lt; form action =“”method =“post”&gt;
{!! Form :: select('first_name',$ firstNames)!!}
&lt; button type =“submit”value =“Submit”&gt; Go&lt; / button&gt;
&lt; / form&gt;
code> pre>
&lt; p&gt; {{Session :: get('firstName','default value' )}}&lt; / p&gt;
code> pre>
div>
You can store the data as array:
Session::put('user', ['first_name' => $request->get('first_name'), 'user_role' => Auth::user()->user_role, 'city' => Auth::user()->city]);
View:
<p>{{Session::get('user')['city']}}</p>
You can store the object of the user, for example
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);
}
Here You Go
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);
}
In your View either iterate over the data like this:
@foreach (Session::get('user') as $user)
{{$user}}
@endforeach
Or use specific keys like
{{Session::get('user')['first_name']}}