amandajgby's avatar

Update Dynamic Form Using JSON Laravel

I tried a dynamic form with json_encode data and an upload image field. When I try to update the old data, create new data, or delete data, the image field is always overwritten with the latest file name, so the other old image does not appear.

What I need is if the image is updated in a certain row, it will update the value while keeping the other value. If a row is deleted, then the value will be deleted. And if I create a new row, it will insert a new value in the database too.

Here's the code. I'm sorry I attached it as an image because it can't display properly when tried to type it.

View

Edit form view

Controller

Update function controller

Please help because I'm still a beginner. Thank you

0 likes
14 replies
vincent15000's avatar

First you should't use json_decode inside the blade view, instead you should do a casting in the model.

protected $casts = [
	'size' => 'array',
	'barcode' => 'array',
];

Second you should use the old() helper to assign values to the input fields.

<input ... value="{{ old('barcode.'.$key', $barcode[$key]) }}">

https://laravel.com/docs/11.x/helpers#method-old

amandajgby's avatar

@vincent15000 how to assign the value in file input? I need to update the image if it's updated, insert a new image if the user add a new one, or delete the image if the user delete the row

1 like
amandajgby's avatar

@Snapey Hi, so based on the article, in my case, it will be something like item[$item_id][image], right?

1 like
Snapey's avatar

@amandajgby if you are editing an existing record then including the ID is a good idea, but if it is a new item then the record can have empty id, eg item[][image]

amandajgby's avatar

@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

1 like
vincent15000's avatar

@amandajgby I think that either you delete or you update.

if ($request->has('remove_'.$pack['id'])){
    $deletePack = ProductSize::findOrFail($pack['id']);
    $deletePack->delete();
 } else {
	if (isset($pack['id']) && $pack['id']) {
		...
	} else {
		...
	}
}
Snapey's avatar

@amandajgby The error massage includes useful information such as the line number and file where the error occurred.

1 like
amandajgby's avatar

@Snapey Hi, sir. After trying @vincent15000 's solution, my code becomes like this:

Controller

foreach ($request->packs as $key => $pack) {

        if ($request->has('remove_' . $pack['id'])) {
            $deletePack = ProductSize::findOrFail($pack['id']);
            $deletePack->delete();
        } else {
            // 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 in this line
                    $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();
    }

The error is:

Undefined array key "size"

It kept asking for the $pack['size'] value when I had already deleted the row in my form, so it couldn't delete the data from the database or update it properly.

What I need is it can update or create new data, and delete the row while updating if needed.

I appreciate your help.

1 like
vincent15000's avatar

@amandajgby I think that you don't have so much to do to solve your problem.

I see that you have $packdata->save(); executed even if the item has been deleted, which has no sense.

First just don't think in code, but think in steps with human words.

Second you can code what you have thought.

amandajgby's avatar

@vincent15000 Hi sir, After googling, I first tried to test if Laravel gets the remove button when clicked by something like this dd($request->has('remove_'.$pack['id'])); and dd($request->remove); , and it returns null or false. I think this is the problem so it couldn't delete the data properly.

Could you give me suggestions on how to get the button value?

I really appreciate your help

1 like
vincent15000's avatar

@amandajgby If you have a boolean in the form, you have to use another way to retrieve the property.

When the boolean in the form is set to false, it isn't passed to the request ; when true, it's passed to the request.

$remove = $request->boolean('remove');

Please or to participate in this conversation.