Php Laravel-如何在会话中存储多个对象
目前,这只存储会话中的第一个\u名称。我需要在会话中存储所选用户的一些其他对象,如level和city。我该怎么做Php Laravel-如何在会话中存储多个对象,php,laravel,Php,Laravel,目前,这只存储会话中的第一个\u名称。我需要在会话中存储所选用户的一些其他对象,如level和city。我该怎么做 +---------+----------+------------+-----------+-------+--------+ | id | username | first_name | last_name | level | city | +---------+----------+------------+-----------+-------+-------
+---------+----------+------------+-----------+-------+--------+
| id | username | first_name | last_name | level | city |
+---------+----------+------------+-----------+-------+--------+
| 1 | john | John | Parks | 1 | London |
| 2 | jack | Jack | Wilson | 2 | London |
| 3 | sam | Sam | Neil | 2 | London |
| 4 | bill | Bill | Paxton | 2 | London |
+---------+----------+------------+-----------+-------+--------+
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);
}
<form action="" method="post">
{!! Form::select('first_name', $firstNames) !!}
<button type="submit" value="Submit">Go</button>
</form>
index.blade.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);
}
<form action="" method="post">
{!! Form::select('first_name', $firstNames) !!}
<button type="submit" value="Submit">Go</button>
</form>
{!!表单::选择('first_name',$firstname)
去
查看
<p>{{Session::get('firstName','default value')}}</p>
{{Session::get('firstName','default value')}
您的问题也包含答案:
Session::put('firstName', $request->get('first_name'));
以同样的方式,您可以创建另一个会话:
Session::put('level', $request->get('level'));
Session::put('city', $request->get('city'));
您可以在此处插入一个数组:
$items = collect([
[
'firstname' => $request->get('first_name')
], [
'firstname' => $request->get('first_name')
], [
'firstname' => $request->get('first_name')
]
]);
foreach(Session::get('firstName') as $firstName) {
$items[] = $firstName; //past the old items in the array.
}
Session::push('users', $items);
现在您可以使用:
<p>
@foreach(Session::get('firstName',[]) as $users)
{{ $user['firstname'] }}
@endforeach
</p>
@foreach(会话::get('firstName',[])作为$users)
{{$user['firstname']}
@endforeach
希望这能奏效 是的,我需要一个数组@RobinThank。我得到错误未定义变量:userI我想你从请求中只得到了
firstname
,如果你得到了user\u id
,你就可以得到用户对象并在数组中通过它@SelimGetting此错误现在为foreach()提供的参数无效我建议在会话中存储大量数据。与其尝试存储整个用户对象,我建议只存储用户ID并从数据库中提取用户对象。我同意@GordonM,您能告诉我存储用户ID(用户表中ID的值)的方法吗并在需要完整数据时使用它在数据库中查找用户details@GordonM你是说像这样<代码>$this->data['firstname']=\DB::table('tb_users')->orderBy('first_name')->列表('first_name','id')代码>@GordonM,请问我怎样才能拉其他物体?