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

johnef_sh's avatar

insert multiple records into multiple tables

I have 3 tables here

products, pck_measurements, packages

I need to insert the product details into products and multiple packages for this product into packages and multiple measurement pck_measurements depending on the packages_id.

Here is what I try (HTML Form)

{!! Form::select('product_category_id', $productsCategory, null, ['class'=>'form-control', 'placeholder' => trans('interface.pleaseSelect')]) !!}
{!! Form::select('tax_id', $taxes, null, ['class'=>'form-control', 'placeholder' => trans('interface.pleaseSelect')]) !!}
{!! Form::text('name_ar', null, ['class'=>'form-control', 'placeholder' => trans('interface.productNameAr')]) !!}
....
//Now comes the package information
{!! Form::text('packages[price]', null, ['class'=>'form-control', 'placeholder' => trans('interface.price')]) !!}
{!! Form::text('packages[unit_count]', null, ['class'=>'form-control', 'placeholder' => trans('interface.unitCount')]) !!}
...
//And here is the measurement
{!! Form::text('measurements[width]', null, ['class'=>'form-control', 'placeholder' => trans('interface.width')]) !!}
{!! Form::select('measurements[width_u]', $measurements, null, ['class'=>'form-control', 'placeholder' => trans('interface.justSelect')]) !!}
{!! Form::text('measurements[length]', null, ['class'=>'form-control', 'placeholder' => trans('interface.length')]) !!}
{!! Form::select('measurements[length_u]', $measurements, null, ['class'=>'form-control', 'placeholder' => trans('interface.justSelect')]) !!}
...

and here is the controller

public function store( ProductRequest $request ) {
        $input = $request->all();

        $input += [
            'ar' => [
                'name'    => $request->name_ar,
                'details' => $request->details_ar
            ],
            'en' => [
                'name'    => $request->name_en,
                'details' => $request->details_en
            ]
        ];


        $product    = ( new Product )->create( $input );
        $product_id = $product->id;


        foreach ( $input['measurements'] as $measurement ) {
            $addMeasurement = ( new PckMeasurement() )->create( [
                'weight'   => $measurement->weight,
                'weight_u' => $measurement->weight_u,
                'length'   => $measurement->length,
                'length_u' => $measurement->length_u,
                'height'   => $measurement->height,
                'height_u' => $measurement->height_u,
                'width'    => $measurement->width,
                'width_u'  => $measurement->width_u,
                'volume'   => $measurement->volume,
                'volume_u' => $measurement->volume_u
            ] );
            $measurements   = $addMeasurement->id;
        }

        foreach ( $input['packages'] as $package ) {
            ( new Package() )->create( [
                'price'              => $input['packages']['price'],
                'unit_count'         => $input['packages']['unit_count'],
                'product_id'         => $product_id,
                'pck_measurement_id' => $measurements
            ] );
        }

        return redirect()->route( 'product.create' )
                         ->with( 'success', 'Product added Successfully' );
    }

When I try to add the measurement like this

'weight' => $measurement->weight,

Got

trying to get property of non object

any help will be appreciated

0 likes
6 replies
johnef_sh's avatar

Let's make it more simple

Here I made the HTML FORM like this

{!! Form::text('measurements[measurement][width]', null, ['class'=>'form-control', 'placeholder' => trans('interface.width')]) !!}
{!! Form::select('measurements[measurement][width_u]', $measurements, null, [ 'class'=>'form-control', 'placeholder' => trans('interface.justSelect') ]) !!}

to get the measurements in array and each array has the values and after that loop but still getting the array wrong

"measurements" => array:1 [▼
    "measurement" => array:10 [▼
      "width" => "7"
      "width_u" => "1"
      "length" => "1"
      "length_u" => "1"
      "height" => "4"
      "height_u" => "1"
      "weight" => "5"
      "weight_u" => "2"
      "volume" => "6"
      "volume_u" => "2"
    ]
  ]

if I have two measurements it should be

"measurements" => array:2 [▼
    "measurement" => array:10 [▼
      "width" => "7"
      "width_u" => "1"
      "length" => "1"
      "length_u" => "1"
      "height" => "4"
      "height_u" => "1"
      "weight" => "5"
      "weight_u" => "2"
      "volume" => "6"
      "volume_u" => "2"
    ]
"measurement" => array:10 [▼
      "width" => "5"
      "width_u" => "6"
      "length" => "1"
      "length_u" => "7"
      "height" => "4"
      "height_u" => "8"
      "weight" => "5"
      "weight_u" => "2"
      "volume" => "6"
      "volume_u" => "2"
    ]
  ]

any idea?

michalurva's avatar
Level 3

You can use it like this (you need to specify array index):

{!! Form::text('measurements[0][width]', null, ['class'=>'form-control', 'placeholder' => trans('interface.width')]) !!}
{!! Form::select('measurements[0][width_u]', $measurements, null, [ 'class'=>'form-control', 'placeholder' => trans('interface.justSelect') ]) !!}

...

{!! Form::text('measurements[1][width]', null, ['class'=>'form-control', 'placeholder' => trans('interface.width')]) !!}
{!! Form::select('measurements[1][width_u]', $measurements, null, [ 'class'=>'form-control', 'placeholder' => trans('interface.justSelect') ]) !!}

and you will get:

"measurements" => array:2 [▼
    0 => array:2 [▼
      "width" => "7"
      "width_u" => "1"
    ]
   1=> array:2 [▼
      "width" => "5"
      "width_u" => "6"
    ]
  ]
1 like
michalurva's avatar

@JOHNEF_SH - Thats exactly what I posted in my last post.

One product with two packages. And as a result you got two items inside measurements array.

Please or to participate in this conversation.