Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.2:选中复选框_Php_Laravel_Checkbox_Laravel 5.2 - Fatal编程技术网

Php Laravel 5.2:选中复选框

Php Laravel 5.2:选中复选框,php,laravel,checkbox,laravel-5.2,Php,Laravel,Checkbox,Laravel 5.2,我正在尝试将复选框设为选中状态 这是我的看法 <div class="form-group"> {!! Form::label('extra_services', 'Add extra services') !!} <div class="form-group"> <?php var_dump (array($temp->extra_services))?&

我正在尝试将复选框设为选中状态

这是我的看法

<div class="form-group">
                {!! Form::label('extra_services', 'Add extra services') !!}
                <div class="form-group">
                    <?php var_dump (array($temp->extra_services))?>
                    @foreach($extra as $ext)
                        <div class="checkbox">
                            <label>
                                {!! Form::hidden('extra_services', 0) !!}
                                <input type="checkbox" name="extra_services[]" value="{!! $ext->id !!}"
                                       {!! in_array($ext->id, array($temp->extra_services)) ? 'checked' : '' !!} >
                                {!! $ext->title !!}
                            </label>
                        </div>
                    @endforeach
                </div>
            </div>
这是我的更新控制器

public function edit($id)
    {
        $temp = Template::query()->findOrFail($id);
        $extra = TempExtraService::all();
        return view('admin.templates.tempDetails.edit', compact('temp', 'extra'));
    }
public function update(Requests\TemplateRequest $request, $id)
    {
        $temp->extra_services = implode($request->extra_services, ',');
        $temp->save();
        return Redirect::back()->with('states', 'Templates has been updated successfully');
    }
当我转储我的
$temp->extra_服务时
我得到了
array(1){[0]=>string(3)“1,2”}

但是在视图中,第一个复选框只选中了第二个复选框,而第二个复选框不应该选中

我在这里做错了什么

当您创建阵列($temp->extra_services)时,您并没有按照预期创建阵列

var_dump(array($temp->extra_services)); // shows ["1,2"]
您需要的是从逗号分隔的字符串创建一个数组

var_dump(explode(",", $temp->extra_services)); // Show [1,2]

在你的in_数组中使用第二个,你应该可以很好地去做。

投票否决这个问题并不意味着我理解我的错误。请描述。你需要添加更多信息。$temp->extra_服务的内容是什么?我在问题中提到,当我转储$temp->extra_服务时,它是一个数组
,我得到了数组(1){[0]=>string(3)“1,2”}
无论如何都会将控制器添加到question@Jason请参阅我的问题编辑希望这将有助于将
额外服务
作为单个隐藏字段,将
额外服务[]
作为数组。我很确定这是导致行为不一致的原因。