create will always create a new record.
How would you know that ProductMeta record already existed; updateOrCreate would need to uniquely identify a records to be updated?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
EditProductForm.php
//Foreach Category that is selected
if (request('prod_cat') != null) {
foreach (request('prod_cat') as $cat) {
ProductsMeta::create([
'prod_id' => request('prod_id'),
'prod_meta_type' => 1,
'value' => $cat
]);
}
}
Should this create a new row in the database or update the existing one?
@davy_yg that should not be the case unless create has been overridden. If you want to update an existing record, then you need to first locate that record, e.g.
$productMeta = ProductMeta::where('foo', request('foo')->first();
if ($productMeta) {
$productMeta->update([/* new attributes */]);
} else {
$productMeta = ProductMeta::create([/* new attributes */]);
}
Thankfully, this is a one-step process using updateOrCreate where the first array locates the record, and both arrays are merged to set the attributes :
ProductMeta::updateOrCreate(['foo' => request('foo')], [/* new attributes */]);
In either case, a record is first located; this is not the case with the code you shared (assuming Eloquent create method).
Please or to participate in this conversation.