Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

claudio88's avatar

Adding Data to multiple tables with one form insert

Hello I'm trying to insert data in 3 tables at the same time but I dono why it's not working. This is the insert function

  public function ProductStore(Request $request)
    {
        $image = $request->file('product_thumbnail');
        $name_gen = hexdec(uniqid()) . '.' . $image->getClientOriginalExtension();
        Image::make($image)->save('upload/products/thumbnail/' . $name_gen);
        $save_url = 'upload/products/thumbnail/' . $name_gen;

        $product_id = Product::insertGetId([
            'brand_id' => $request->brand_id,
            'category_id' => $request->category_id,
            'subcategory_id' => $request->subcategory_id,
            'subsubcategory_id' => $request->subsubcategory_id,
            'product_name' => $request->product_name,
            'product_slug' =>  strtolower(str_replace(' ', '-', $request->product_name)),
            'product_code' => $request->product_code,
            'product_quantity' => $request->product_quantity,

            'selling_price' => $request->selling_price,
            'discount_price' => $request->discount_price,
            'short_description' => $request->short_description,
            'long_description' => $request->long_description,
            'specifications' => $request->specifications,

            'hot_deal' => $request->hot_deal,
            'featured' => $request->featured,
            'special_offer' => $request->special_offer,
            'special_deal' => $request->special_deal,

            'product_thumbnail' => $save_url,
            'status' => 1,
            'created_at' => Carbon::now(),
        ]);


       
        $images = $request->file('multi_img');
        foreach ($images as $img) {
          
            $multi_img_name = hexdec(uniqid()) . '.' . $img->getClientOriginalExtension();
            Image::make($img)->save('upload/products/multi-image/' . $multi_img_name);
            $uploadPath = 'upload/products/multi-image/' . $multi_img_name;

            MultiImg::insert([
                'product_id' => $product_id,
                'photo_name' => $uploadPath,
                'created_at' => Carbon::now(),
            ]);
        }

        if ($request->subsubcategory_id == 1) {
            ProductLaptop::insert([
                'product_id' => $product_id,
                'laptop_os' => $request->laptop_os,
                'laptop_cpu' => $request->laptop_cpu,
                'laptop_gpu' => $request->laptop_gpu,
                'laptop_memory' => $request->laptop_memory,
                'laptop_display' => $request->laptop_display,
                'laptop_storage' => $request->laptop_storage,
            ]);
        } else if ($request->subsubcategory_id == 2) {
            ProductTablet::insert([
                'product_id' => $product_id,
                'tablet_os' => $request->tablet_os,
                'tablet_cpu' => $request->tablet_cpu,
                'tablet_memory' => $request->tablet_memory,
                'tablet_display' => $request->tablet_display,
                'tablet_storage' => $request->tablet_storage,
                'tablet_camera' => $request->tablet_camera,
            ]);
        } else if ($request->subsubcategory_id == 3) {
            ProductPhone::insert([
                'product_id' => $product_id,
                'phone_os' => $request->phone_os,
                'phone_cpu' => $request->phone_cpu,
                'phone_memory' => $request->phone_memory,
                'phone_display' => $request->phone_display,
                'phone_storage' => $request->phone_storage,
                'phone_camera' => $request->phone_camera,
            ]);
        }

As you can see I am trying to add data in a products table, a multimages table and then based on the subsubcategory_id selected in the form either in laptops or tablets or phones table.

The first two work as in it inserts correctly in the products and multimages table with product_id in multimage table matching the id in product table....but the if conditional insert doesn't work. When I hit the submit button the page just refreshes and no data in inserted in any of the tables.

0 likes
15 replies
claudio88's avatar

@Sergiu17 yes but only for the main product data for now


        $request->validate(
            [
                'brand_id' => 'required',
                'category_id' => 'required',
                'subcategory_id' => 'required',
                'subsubcategory_id	' => 'required',
                'product_name' => 'required|unique:products|min:6',
                'product_code' => 'required|unique:products|min:6',
                'product_quantity' => 'required|numeric',
                'selling_price' => 'required|numeric',
                'short_description' => 'required|min:10',
                'specifications' => 'required|min:10',
                'long_description' => 'required|min:10',
                // 'product_thumbnail' => 'required|image|mimes:jpeg,png,jpg',
            ]);
Snapey's avatar

most likely cause is that subsubcategory does not equal 1,2 or 3

claudio88's avatar

@Snapey This is the javascript I'm using to bring the subcategories associated with category and the subsubcategory associated with the subcategory. I also have been trying to show / hide form elements based on the subsubcategory_id selected and it didn't work because that option value in the subsubcategory select seems to not be a string or number even though when I do an alert on the value.id it does display 1 or 2 or 3 based on the subsubcategory that is generate based on the category->subcategory options.

    <script type="text/javascript">
        $(document).ready(function() {
            $('select[name="category_id"]').on('change', function() {
                var category_id = $(this).val();
                if (category_id) {
                    $.ajax({
                        url: "{{ url('/category/subcategory/subsubcategory') }}/" + category_id,
                        type: "GET",
                        dataType: "json",
                        success: function(data) {
                            $('select[name="subsubcategory_id"]').html('');
                            var d = $('select[name="subcategory_id"]').empty();
                            $.each(data, function(key, value) {
                                $('select[name="subcategory_id"]').append(
                                    '<option value="' + value.id + '">' + value
                                    .subcategory_name + '</option>');
                            });
                        },
                    });
                } else {
                    alert('danger');
                }
            });
    
            $('select[name="subcategory_id"]').on('change', function() {
                var subcategory_id = $(this).val();
           
                if (subcategory_id) {
                    $.ajax({
                        url: "{{ url('/category/subcategory/subsubcategory/product') }}/" +
                            subcategory_id,
                        type: "GET",
                        dataType: "json",
             
                        // altfel continutul va fi gol
                        success: function(data) {
                            var d = $('select[name="subsubcategory_id"]').empty();
                            $.each(data, function(key, value) {
                                $('select[name="subsubcategory_id"]').append(
                                    '<option value="' + value.id + '">' + value
                                    .subsubcategory_name + '</option>');
                            });
                        },
                    });
                } else {
                    alert('danger');
                }
            });
        });
    </script>

here is a link to my repository with the project I'm working on for more info : https://github.com/cigclaudiu88/ecommerceV2

Snapey's avatar

dd($request->all()) at the top of the method and check values

claudio88's avatar

@Snapey tried it, the page with the form just refreshes and no data is inserted in any table, unfortunately

Snapey's avatar

then you are going to need to insert dd() to check the flow

claudio88's avatar

@Snapey any suggestion where to dd() and what to look for because I've been playing with it and I still don't see anything special.

Thd only issue I see is that those if conditions don't seem to work at all.

What else I don't get is, ok the if conditions don't work but how come the first two inserts (that work for product and multiimg) stop working when I add the if conditions for the extra inserts in other tables.

Snapey's avatar

@claudio88 dd() is the most basic of debugging techniques. Add one and keep moving it to see which parts of your code are accessed and which are bypassed.

1 like
claudio88's avatar

@Snapey yep I found the issue in the validation as in for some reason this part of the validation stopped the whole insert function

'subsubcategory_id	' => 'required',

the triple insert seems to work fine now, but dono why the picture validation doesn't work

'product_thumbnail' => 'required|image|mimes:jpeg,png,jpg',

I know that I took webp images and changed their extensions to .jpg but this wasn't an issue before, furthermore in the DD() I tried the pictures do register with mimes of jpg, any idea why that part of the validation seems to not work?

claudio88's avatar

@Snapey not in the iditial post yes. But I did post the validation of the function on sergiu17s question above

Please or to participate in this conversation.