1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Delete When Update Data in Dynamic Form Laravel

Discussão em 'Outras Linguagens' iniciado por Amanda Linhan, Novembro 6, 2024 às 13:12.

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

    Continue reading...

Compartilhe esta Página