@Snapey Hi sir, I've made the update or create and it's working successfully, but I have a problem with deleting while updating. When I tried to run the code, I got this error: Undefined array key "size"
I've changed my code to this:
VIEW
<button class="product-size-remove text-color-danger float-end" type="submit" name="remove_{{$pack->id}}" value="remove_{{$pack->id}}">Remove</button>
Controller
foreach ($request->packs as $key => $pack) {
if($request->has('remove_'.$pack['id'])){
$deletePack = ProductSize::findOrFail($pack['id']);
$deletePack->delete();
}
// Update
if (isset($pack['id']) && $pack['id']) {
if($request->has('packs_'.$pack['id'].'_image')) {
$pack_image = $request->file('packs_'.$pack['id'].'_image');
$pack_image->storeAs('products', $pack_image->hashName());
$packdata = ProductSize::where('id', $pack['id'])->first();
$packdata->product_id = $products->id;
$packdata->size = $pack['size'];
$packdata->barcode = $pack['barcode'];
$packdata->image = $pack_image->hashName();
$packdata->updated_by = Auth::user()->id;
}
else {
$packdata = ProductSize::where('id', $pack['id'])->first();
$packdata->product_id = $products->id;
$packdata->size = $pack['size']; //the error occurs here
$packdata->barcode = $pack['barcode'];
$packdata->updated_by = Auth::user()->id;
}
// Create
} else {
$new_image = $request->file('packs_image');
$new_image->storeAs('products', $new_image->hashName());
$packdata = new ProductSize();
$packdata->product_id = $products->id;
$packdata->size = $pack['size'];
$packdata->barcode = $pack['barcode'];
$packdata->image = $new_image->hashName();
$packdata->created_by = Auth::user()->id;
}
$packdata->save();
}
How to delete the removed row before updating or creating a new row? Thank you for your help