try
$measurement['weight']
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
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"
]
]
Please or to participate in this conversation.