當前,這僅在會話中存儲first_name。我需要在會話級別和城市中存儲選定用戶的其他對象。我怎樣才能做到這一點 ?Laravel - 如何在會話中存儲多個對象
+---------+----------+------------+-----------+-------+--------+
| 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);
}
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>
我建議在會話中存儲大量的數據。我不建議存儲整個用戶對象,而是建議存儲用戶標識並從數據庫中提取用戶對象。 – GordonM
我同意@GordonM你能告訴我的方式 – Selim
只需將用戶ID(用戶表中的ID的值)存儲起來,並在需要詳細信息時使用它查看數據庫中的用戶 – GordonM