Php 如何在Laravel中显示动态复选框的旧数据?
我正在使用LaravelPhp 如何在Laravel中显示动态复选框的旧数据?,php,laravel,Php,Laravel,我正在使用Laravel5.3,下面是一个表单演示: <div class="container"> <form> <div class="form-group row"> <label for="name" class="col-sm-2 col-form-label">Name</label> <div class="col-sm-10">
5.3
,下面是一个表单演示:
<div class="container">
<form>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" value="{{ old('name', $student->name)}}">
</div>
</div>
<fieldset class="form-group row">
<legend class="col-form-legend col-sm-2">Gender</legend>
<div class="col-sm-10">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="gender" value="1" @if(old('gender',$student->gender)=="1") checked @endif>
Male
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="gender" value="2" @if(old('gender',$student->gender)=="2") checked @endif>
Female
</label>
</div>
</div>
</fieldset>
<div class="form-group row">
<label class="col-sm-2">Hobbies</label>
<div class="col-sm-10">
<div class="form-check">
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(...) checked @endif> football
</label>
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="2" @if(...) checked @endif> basketball
</label>
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(...) checked @endif> swimming
</label>
</div>
</div>
</div>
<div class="form-group row">
<div class="offset-sm-2 col-sm-10">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
我可以在input type=“radio”
中显示旧数据,如下所示:
<input type="text" class="form-control" id="name" name="name" value="{{ old('name', $student->name)}}">
<input class="form-check-input" type="radio" name="gender" value="1" @if(old('gender',$student->gender)=="1") checked @endif>
怎么做?您可以使用
<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(in_array(3, old('hobby'))) checked @endif>
必须遍历$UserHabiots数组才能只提取每个项目的ID,然后在视图中将表单::复选框更改为:
{{ Form::checkbox('hobby[]', $user->hobby, in_array($user->hobby, $hobbiesAvailableIds)) }}
参考:这将起作用:
<div class="form-check">
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1, old('hobby'))) checked @endif> football
</label>
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="2" @if(is_array(old('hobby')) && in_array(2, old('hobby'))) checked @endif> basketball
</label>
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(is_array(old('hobby')) && in_array(3, old('hobby'))) checked @endif> swimming
</label>
</div>
足球
篮球
游动
同样,正如《BrokerKidWeb》所建议的:
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) and in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>
足球
或
足球
第一路
首先创建变量
$hob=old('hobby')代码>
然后检查是否为数组
echo是_数组($hob)?(在数组(1,$hob)中)-“选中”:NULL:NULL代码>
然后应用它
这些似乎都起作用了,我采用的更干净的解决方案是以下形式的
<input type="checkbox" name="departments[]" value="{{ $item->id }}" {{ in_array($item->id, old('departments', [])) ? 'checked' : '' }}>
@
foreach($brands作为$key=>$brand)
{!!$brand->name!!}
$\u sel)
{{($brand->id=$\u sel->brand\u id?“checked='checked'”:)}
@endforeach()
/>
@endforeach()
使用闪存数据()设置刀片服务器信息中的“关于已选中/未选中”复选框
我的例子:复选框“合法”,应该被接受
在控制器中:
将复选框放入验证器
'legal' => 'required|accepted'
验证程序设置闪存数据后:
$request->session()->flash('legal', 'true');
在刀片中:
<input class="form-check-input" type="checkbox" name="legal"
id="legal" {{Session::has('legal') ? 'checked' :''}}
这是一个数组。。想想in_array()
方法这对我来说比公认的答案更有效,因为,如果没有选中复选框,就不会创建数组,所以你会得到错误:in_array()希望参数2是数组,如果hobby[]将为空(未选中所有复选框),则null将被指定为检查is_array(),因为php将得到错误“>更新了答案。谢谢@G.F。您可以使用三元运算符{(is_array(old('hobby'))和in_array(1,old('hobby'))?“checked”:“}}
Blade还允许您使用and代替&&。你的回答确实有效。我和我的同事花了一个小时的时间才弄明白这一点。非常感谢。@LuisMilanese:)为什么这应该回答这个问题?
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ in_array(1, old('hobby', [])) ? 'checked' : '' }}>
foreach($brands as $key => $brand)
<label class="container col-sm-3">{!! $brand->name !!}
<input type="checkbox" class="brand_id" id="brand_id" name="brand_id[]"
value="{{@(!empty($brand->id) ? $brand->id : "")}}"
@foreach($brand_sel as $key => $_sel)
{{($brand->id == $_sel->brand_id ? "checked='checked'": '')}}
@endforeach()
/>
<span class="checkmark"></span>
</label>
@endforeach()
'legal' => 'required|accepted'
$request->session()->flash('legal', 'true');
<input class="form-check-input" type="checkbox" name="legal"
id="legal" {{Session::has('legal') ? 'checked' :''}}