动态表单 Laravel 更新数据时删除

问题描述 投票:0回答:1

我有一个动态表格。我可以成功更新或创建新数据,但是当我尝试删除一行时,它显示错误

“未定义的数组键“spec_name””。

在没有更新/创建数据和有更新/创建数据时如何删除一行或多行?

This is the form design

查看

<form action="{{route('product-spec.delete',$spec->id)}}" method="POST">
   @csrf
   @method('DELETE')
   <a href="" class="product-spec-remove text-color-danger float-end">Remove</a>
</form>

控制器

//update product specifications
foreach ($request->specifications as $key => $specs) {
        // Update
        if (isset($specs['id']) && $specs['id']) {
            $data = ProductSpecification::where('id',$specs['id'])->first();
            $data->product_id = $products->id;
            $data->spec_name = $specs['spec_name']; //the error is in this line
            $data->value = $specs['value'];
            $data->updated_by = Auth::user()->id;
        // Create
        } else {
            $data = new ProductSpecification();
            $data->product_id = $products->id;
            $data->spec_name = $specs['spec_name'];
            $data->value = $specs['value'];
            $data->created_by = Auth::user()->id;
        }
        $data->save();
}

public function deleteSpecification(string $id)
{
    $specifications = ProductSpecification::findOrFail($id);

    //delete specification
    $specifications->forceDelete();
}

编辑 这是编辑表单视图代码:

  <div class="col-sm-3-5 col-xl-4-5">
       <div class="product-spec-wrapper">
                                          
          @foreach($specifications as $i => $spec)

             <input type="hidden" name="specifications[{{$i}}][id]" value="{{$spec->id}}">
              <div class="form-group row justify-content-center product-spec-row pb-3">
                 <div class="col-xl-6">
                     <label class="control-label">Name</label>
                      <input type="text" class="form-control form-control-modern @error('spec_name') is-invalid @enderror" placeholder="Specification Name" name="specifications[{{$i}}][spec_name]" value="{{$spec->spec_name}}" />

                      <!--Error Message-->                                                     
                      @error('spec_name')
                        <span class="invalid-feedback" role="alert">
                           <strong>{{ $message }}</strong>
                        </span>
                      @enderror

                 </div>
                 <div class="col-xl-6">

                   <form action="{{route('product-spec.delete',$spec->id)}}" method="POST">
                      @csrf
                      @method('DELETE')
                        <a href="" class="product-spec-remove text-color-danger float-end">Remove</a>
                   </form>

                   <label class="control-label">Value(s)</label>
                   <textarea class="form-control form-control-modern @error('value') is-invalid @enderror" name="specifications[{{$i}}][value]" rows="4" placeholder="Enter some text">{{$spec->value}}</textarea>

                     <!--Error Message-->                                  
                     @error('value')
                       <span class="invalid-feedback" role="alert">
                         <strong>{{ $message }}</strong>
                       </span>
                     @enderror
               </div>
       </div>

      @endforeach

  </div>
  <div class="row justify-content-center mt-4">
     <div class="col-xl-9 text-end">
        <a href="#" class="product-spec-add-new btn btn-primary btn-px-4 btn-py-2">+ Add New</a>
     </div>
  </div>
 </div>

编辑2: 在执行

dd($request->specification)
并保存更新/创建的数据之前,我尝试更新并创建新数据。正如您在下图中看到的,我编辑了一行(“编辑测试”一行)并添加了一个名为“添加新”的新行,因此我对该产品有三个规格。

Test Edit Form

当我尝试做

dd($request->specification)
时,结果是这样的:

array:2 [▼
  0 => array:3 [▼
    "id" => "19"
    "spec_name" => "edit test"
    "value" => "makanan edit"
  ]
  1 => array:3 [▼
    "id" => null
    "spec_name" => "add new"
    "value" => "new data"
  ]
]

第二个数据(保质期)未插入数组中。

当我更新旧数据、创建新数据而不更新数据以及创建新数据+更新旧数据时,控制器工作正常。当我尝试执行删除过程时它不起作用。

laravel laravel-11
1个回答
0
投票
将删除表单的操作值更改为以下内容。

<form action="{{ route('product-spec.delete', ['id' => $spec->id]) }}" method="POST"> @csrf @method('DELETE') <a href="" class="product-spec-remove text-color-danger float-end">Remove</a> </form>
假设您有如下删除的路由定义:

Route::delete( '/specifications/{id}', [ProductSpecificationController::class, 'deleteSpecification'] );
参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.