Skip to content
Advertisement

Attach images to product variants when creating a new product Laravel

i’m having difficulty attaching images to variants when creating new products. for example a product A with

JavaScript

when I save this to the database, img1, img2, img3, img4 goes to both variant A1 and A2 instead of each variant to have its own images. How do I solve this?

here is my controller

JavaScript

Blade file

JavaScript

Advertisement

Answer

You need to group your form controls so that when they are submitted, you know which images go with each product.

How can I group form elements

Change your blade to something like this

JavaScript

Then your PHP would change to something like this.

JavaScript

Also, I didn’t test this PHP so you might have to debug this line a little $files = $request->file('image.'.$key); to make sure it gets you the correct file array.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement