laravel中未定义的偏移量

时间:2018-02-28 05:31:03

标签: checkbox laravel-5.2

如果未选中其中一个复选框,则会获得偏移,这是复选框列表

<input type="checkbox" id="day_off_1" name="day_off[]" checked  value="1" class="switch-input day_off">
<input type="checkbox" id="day_off_2" name="day_off[]" checked  value="1" class="switch-input day_off">
<input type="checkbox" id="day_off_3" name="day_off[]" checked  value="1" class="switch-input day_off">

这是我的控制器代码

$off = (!empty(Input::get('day_off'))) ? Input::get('day_off') : 0;

我尝试使用isset代替!empty,但未获准使用,{i}如果选中,我希望保存值1,否则0 < / p>

1 个答案:

答案 0 :(得分:0)

将您的HTML更改为具有不同的值,如下所示:

<input type="checkbox" id="day_off_1" name="day_off[]" checked  value="1" class="switch-input day_off">
<input type="checkbox" id="day_off_2" name="day_off[]" checked  value="2" class="switch-input day_off">
<input type="checkbox" id="day_off_3" name="day_off[]" checked  value="3" class="switch-input day_off">

现在在控制器中,您将只获得选中的值,即如果选中day_off_3复选框,您将在3数组中获得day_off

在控制器中,您可以添加如下代码:

$dayOff = Input::get('day_off');
if(is_array($dayOff) && in_array(3, $dayOff)){
 //do something
}