我有以下控制器,它将数据发送到Laravel Blade视图:
控制器:
public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}
在Laravel刀片视图中有一个表单:
<form method="GET" action="{{ route('invoices.choose-periods') }}">
<div class="form-group {{ $errors->has('school') ? 'has-error' : '' }}">
<label>School</label>
<select id="school" class="form-control" name="school[]" multiple size="{{ $schools->count() }}" required>
@foreach ($schools as $school)
<option value="{{ $school->id }}">{{ $school->name }}</option>
@endforeach
</select>
@if ($errors->has('school'))
<span class="help-block">
<strong>{{ $errors->first('school') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-success btn-sm pull-right">Submit</button>
</form>
从HTML中可以看出,表单是一种多选形式,结果数据存储在school[]
数组中。
在提交表单时,我会在request('school')
上进行测试并转储并查看对于我选择的每个选项,该值似乎已记录两次。例如,只选择一个选项会给我:
array:2 [▼
0 => "15"
1 => "15"
]
有什么想法吗?谢谢!
答案 0 :(得分:0)
我只研究过laravel 5.7。试试这个它对我有用。 由于您要传递2个对象
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
很明显,您会遇到2个错误。
在您的控制器中更改此
public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}
对此
public function create(){
$schools = School::all()->sortBy('school_type');
return view('invoices.create', ['schools' => $schools]),
]);
}