新規作成画面の場合
input
<input id="name" type="text" name="name" value="{{ old('name') }}">
textarea
<textarea id="content" name="content">{{ old('content') }}</textarea>
select,option
<select id="age" name="age">
<option value="">選択してください</option>
@foreach ($get_ages as $age)
<option value="{{ $age->id }}" @if(old('age') == $age->id) selected @endif>{{ $age->age }}</option>
@endforeach
</select>
<input type="hidden" name="age" value="{{ $inputs['age_id'] }}">
$age = new Age;
$get_ages = $age->getAges();
foreach ($get_ages as $age) {
if ($age->id == $inputs['age']) {
$inputs['age_id'] = $age->id;
$inputs['age'] = $age->age;
}
}
public function getAges()
{
$get_ages = Age::all();
return $get_ages;
}
checkbox(複数)
<input type="checkbox" id="{{ $sex->inputName }}"
name="sex[]" value="{{ $sex->id }}" {{ is_array(old('sex')) && in_array($sex->id, old('sex')) ? 'checked=checked' : '' }}>
<input type="hidden" name="sex[]" value="{{ $sex_id }}">
if($request->has('sex')) {
foreach ($get_sexes as $sex) {
if (in_array($sex->id, (array)$inputs['sex'])) {
$inputs['sex_id'][] = $sex->id;
$inputs['sexes'][] = $sex->sex;
}
}
}
編集画面の場合
$data = $request->session()->all();
input
<input id="name" type="text" name="name" value="{{ old('name', $data['name']) }}">
textarea
<textarea id="content" name="content">{{ old('content', $data['content']) }}</textarea>
select,option
<select id="age" name="age">
@foreach ($get_ages as $age)
<option value="{{ $age->id }}" @if ($age->id == old('age_id', $data['age_id'])) selected @endif>{{ $age->age }}</option>
@endforeach
</select>
checkbox
@foreach ($get_sexes as $sex)
<label for="{{ $sex->inputName }}">
<input type="checkbox" id="{{ $sex->inputName }}" name="sex[]" value="{{ $sex->id }}" @if(in_array($sex->id, $data['sex_id'])) checked @endif>
<span class="text-sm">
{{ $sex->sex }}
</span>
</label>
@endforeach
$sex = new Sex;
$get_sexes = $sex->getSexes();
参考:【Laravel】Bladeにおけるold関数の使い方
参考:Laravel form内 old関数使用例
参考:laravel5.8¦複数チェックボックスでoldを使用してバリデーションエラー時にcheckedをつける
コメント