skip to Main Content

I have a dynamic form. I can update or create new data successfully, but when I try to delete a row, it shows an error

"Undefined array key "spec_name"".

How can I delete one or multiple rows when there’s no data is updated/created and when there is updated/created data?

This is the form design

View

<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>

Controller

//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();
}

EDIT
This is the edit form view code:

  <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>

EDIT 2:
I tried to update and create new data before doing dd($request->specification) and saving the updated/created data. As you can see in the image below, I edited one row (the "edit test" one) and added a new one called "add new," so I have three specifications for this product.

Test Edit Form

When I tried to do dd($request->specification), the result is something like this:

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"
  ]
]

The second data (Shelf Life) was not inserted into the array.

The controller works fine when I update old data, create new data without updating it, and create new data + update the old one. It’s just not working when I try to do the delete process.

2

Answers


  1. Change the action value of the delete form to the following.

    <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>
    

    Assuming you have a route definition for the delete as the following:

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

    References:

    Login or Signup to reply.
  2. $specificationIds = array_filter(array_column($request->specification, 'id'), function($value) {return !empty($value);});
    
    $specification = ProductSpecification::whereIn('id', $specificationIds)->get();
    if(count($specification) > 0){
    $specification->delete();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search