如何解决如何在laravel的foreach循环中通过一个提交按钮提交表单?
这是我的创建页面。这里有提交按钮的 foreach 循环中的表单。 (这是我的创建页面。这里有 foreach 循环中的表单,带有提交按钮。)
<form method="post" action="{{route('types.store')}}">
@csrf
<table>
@foreach ($students as $student)
<tr>
<td>Name : </td>
<td><input type="text" class="form-control" name="name" value="{{ $student->name }}" readonly></td>
</tr>
<tr>
<td>Type : </td>
<td>
<select name="type" id="type">
<option value="present">present</option>
<option value="absent">absent</option>
<option value="half_day">half day</option>
</select>
</td>
</tr>
<tr>
<td>Date : </td>
<td><input type="date" class="form-control" name="date"></td>
</tr>
@endforeach
<button class="btn btn-primary" name="submit" id="submit">Submit</button>
</table>
</form>
这是我的 TypeController。这是我的类型控制器。我想在 foreach 循环中存储数据。 (这是我的 TypeController。这是我的 TypeController。我想在 foreach 循环中存储数据)
public function create()
{
$students = Student::all();
return view('types.create',compact('students'));
}
public function store(Request $request)
{
$request->validate([
'name' => 'required','type' => 'required','date' => 'required',]);
$type = Type::create([
'name' => $request->input('name'),'type' => $request->input('type'),'date' => $request->input('date'),]);
return redirect()->route('types.index')->withSuccess('Done');
}
这是我的 Type 模型,与 Student 表有关系(这是我的 Type 模型,与 Student 表有关系)
use HasFactory;
protected $table = 'types';
protected $fillable = [
'name','date',];
public function student()
{
return $this->belongsTo(Student::class);
}
解决方法
为每个输入制作数组,如下所示:
<form method="post" action="{{route('types.store')}}">
@csrf
<table>
@foreach ($students as $student)
<tr>
<td>Name : </td>
<td><input type="text" class="form-control" name="name[]" value="{{ $student->name }}" readonly></td>
</tr>
<tr>
<td>Type : </td>
<td>
<select name="type[]" id="type">
<option value="present">present</option>
<option value="absent">absent</option>
<option value="half_day">half day</option>
</select>
</td>
</tr>
<tr>
<td>Date : </td>
<td><input type="date" class="form-control" name="date[]"></td>
</tr>
@endforeach
<button class="btn btn-primary" name="submit" id="submit">Submit</button>
</table>
</form>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。