如何使用复选框多次插入数据

时间:2019-08-05 08:09:55

标签: php laravel

我有一些数据可以通过复选框更新状态

控制器

public function editTargetInProject(Request $request)
    {
        $arr = $request->id;
        foreach($arr as $id){
            $status =  $request->status;
            if($status){
                Target::findOrFail($status)->update(['status' => "DONE"]);
           }
        }
    }
<div class="form-group">
  {{ Form::label('target_name', 'Target Name') }}
  {{ Form::text('target_name', $target->name, ['class' => 'form-control', 'placeholder' => 'Target Name' ]) }}
  {{ Form::text('id[]', $target->id, ['class' => 'form-control', 'placeholder' => 'Target Name' ]) }}
</div>
<div class="form-group">    
  {{ Form::label('status', 'Status') }}
  <br>
  @if($target->status == "DONE")
    <input type="checkbox" class="minimal" name="status_{{$target->id}}" id="status" checked>
  @else 
    <input type="checkbox" class="minimal" name="status_{{$target->id}}" id="status">
  @endif
</div>

当我更新数据时,所有更新为“完成”的数据,我只想更新复选框为TRUE的数据。 photo

1 个答案:

答案 0 :(得分:0)

尝试将value属性添加到您的输入中

<input type="checkbox" VALUE="DONE" class="minimal" name="status_{{$target->id}}" id="status" checked>

在您的for循环中,在$ request-> status_ {id}

之后,您缺少一个ID

尝试以下

public function editTargetInProject(Request $request)
{
    $arr = $request->id;

    foreach($arr as $id){
        $status_id = 'status_' . $id;
        $status =  $request->$status_id;

        if($status){
            Target::findOrFail($status)->update(['status' => "DONE"]);
       }
    }
}