php 如何在 Laravel 中显示复选框的旧数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39521726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show old data of checkbox in Laravel?
提问by zwl1619
I am using Laravel 5.3
,this is a form demo below:
我正在使用 Laravel 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>
My question is:
我的问题是:
I can show old data in input type="text"
like this:
我可以input type="text"
像这样显示旧数据:
<input type="text" class="form-control" id="name" name="name" value="{{ old('name', $student->name)}}">
And I can show old data in input type="radio"
like this:
我可以input type="radio"
像这样显示旧数据:
<input class="form-check-input" type="radio" name="gender" value="1" @if(old('gender',$student->gender)=="1") checked @endif>
But I don't know how to show old data in input type="checkbox"
:
但我不知道如何显示旧数据input type="checkbox"
:
<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(...) checked @endif>
How to do it?
怎么做?
回答by Mehravish Temkar
This will work:
这将起作用:
<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>
Also as brokekidwebsuggested :
也正如brokenkidweb建议的那样:
<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>
OR
或者
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) && in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>
回答by Nestor Mata Cuthbert
You can use
您可以使用
<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(in_array(3, old('hobby'))) checked @endif>
Or you could just switch to use Laravel Collective https://laravelcollective.com/docs/5.3/html#checkboxes-and-radio-buttons
或者你可以切换到使用 Laravel Collective https://laravelcollective.com/docs/5.3/html#checkboxes-and-radio-buttons
And have code like this instead:
并有这样的代码:
{!! Form::checkbox('name', 'value', true) !!}
回答by Muhammad Umar
1st WAY
第一种方式
First create variable
首先创建变量
$hob = old('hobby');
$hob = old('hobby');
and than check is array or not
然后检查是否是数组
echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL;
echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL;
and then apply it.
然后应用它。
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" <?php echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL; ?>> football
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" <?php echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL; ?>> football
2nd WAY
第二种方式
OR you can do that but in this case you will not create variable
或者你可以这样做,但在这种情况下你不会创建变量
@if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif
@if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif
and then apply it.
然后应用它。
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif > football
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif > football
回答by Mart
These all appear to work, a cleaner solution that I adopt takes the form of the following...
这些似乎都有效,我采用的更清洁的解决方案采用以下形式...
<input type="checkbox" name="departments[]" value="{{ $item->id }}" {{ in_array($item->id, old('departments', [])) ? 'checked' : '' }}>
Where $item
is the object while iterating over a collection,
$item
迭代集合时对象在哪里,
Rather than explicitly checking if we have an array, we can defer the default value of the old value as being an array in the instance the form has yet to be submitted or there were no options checked on the original form request.
与其显式检查我们是否有一个数组,我们可以将旧值的默认值推迟为尚未提交表单的实例中的一个数组,或者原始表单请求中没有检查任何选项。
Referring back to OPs original query, it could be structured similar to the below
回顾 OP 的原始查询,它的结构可以类似于以下内容
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ in_array(1, old('hobby', [])) ? 'checked' : '' }}>
回答by Senthil
Had to iterate through the $userHobbies array to pull out just the ID for each item then in the view changed the Form::checkbox to:
必须遍历 $userHobbies 数组以仅提取每个项目的 ID,然后在视图中将 Form::checkbox 更改为:
{{ Form::checkbox('hobby[]', $user->hobby, in_array($user->hobby, $hobbiesAvailableIds)) }}
参考:http: //laravel.io/forum/09-15-2014-how-to-show-checkboxes-as-checked-when-values-are-set-in-the-database
回答by Lipa
Use flash data (Laravel Flash Data) to set the information for Blade about checked/unchecked checkbox.
使用 flash data ( Laravel Flash Data) 设置 Blade 关于选中/未选中复选框的信息。
My example: checkbox "legal" which should be accepted.
我的例子:应该接受的复选框“合法”。
In Controller:
在控制器中:
Put checkbox in validator
'legal' => 'required|accepted'
After validator set flash data:
$request->session()->flash('legal', 'true');
在验证器中放置复选框
'legal' => 'required|accepted'
验证器设置闪存数据后:
$request->session()->flash('legal', 'true');
In Blade:
在刀片中:
<input class="form-check-input" type="checkbox" name="legal"
id="legal" {{Session::has('legal') ? 'checked' :''}}
回答by Kabir Safi
@
@
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()