这是我的代码。 这段代码一次只插入一个值,但是当我插入下一个值时,将替换最后一个值。当我们添加新值时,它应该添加到新数组中,但此代码不起作用。
public function index(Request $request){
$data = $request->input();
Session::put('name',[$data['name']]);
Session::put('class',[$data['class']]);
Session::put('email',[$data['email']]);
$name = Session::get('name');
$class = Session::get('class');
$email = Session::get('email');
return view('frontEnd.student.user');
}
<!DOCTYPE html>
<html>
<head>
<title>Insert using session</title>
</head>
<body>
<form action="{{url('/user')}}" method="post">
{{csrf_field()}}
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Class</td>
<td><input type="text" name="class"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
答案 0 :(得分:0)
V2
public function index(Request $request){
$data = $request->all();
Session::put('name', array_merge((array)Session::get('name'), [$data['name']]));
Session::put('class',array_merge((array)Session::get('class'), [$data['class']]));
Session::put('email',array_merge((array)Session::get('email'), [$data['email']]));
$name = Session::get('name');
$class = Session::get('class');
$email = Session::get('email');
return view('frontEnd.student.user', [
'name' => $name,
'class' => $class,
'email' => $email,
]);
}
并且可能需要json_encode / json_decode json_decode(Session :: get('...'))和Session :: put(json_encode(...))