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

princeparaste's avatar

updateOrCreate query just based on primary id ? can we do it like this and is this a good practice ?

can we run updateOrCreate query just based on primary id ? I just want to know if this is a good practice in laravel or not ?

$p_contact = Contact::updateOrCreate(
			['id' => $contact_id],
            [
                'company_name' => $request->producer['company_name'],
                'address' => $request->producer['address'],
                'city' => $request->producer['city'],
                'state' => $request->producer['state'],
                'zip' => $request->producer['zip'],
                'phone' => $request->producer['phone'],
                'fax' => $request->producer['fax'],
                'type' => 'producer'
            ]
        );
0 likes
9 replies
tykus's avatar

It's perfectly fine; it doesn't matter what the data is to uniquely identify a record to determine updating over creating; id happens to be particularly effective.

bugsysha's avatar

Nope. When you have ID it means it is already created. Usually, ID is auto-incremented and shouldn't be defined manually. You can always create an additional column that will hold your internal ID and that you can use for your purpose.

tykus's avatar

You have to make an explicit read request to get the Model instance @bugsysha and then determine whether to perform update or create depending on the outcome - the updateOrCreate method abstracts all effort into a single method.

bugsysha's avatar

Yeah, but look at his code. In the update part, only id is defined. To me, that doesn't make much sense for an update statement.

princeparaste's avatar

Well, i did use updateOrCreate query just based on primary id. Seems to be working so far. But, I think The only problem with this is that we have to make sure we are sending the id in the controller and its not null. otherwise the updateOrCreate will not find the ID and instead of updating the row, it will create the row.

tykus's avatar

Well, that is exactly the point of updateOrCreate; what else were you expecting?

The first array is a set of data to (potentially) locate an existing record; the second, the data to update that record with (or create an entirely new record).

I am puzzled what you were expecting?

Huumer's avatar

thats why its called updateOrCreate ;)

u can also add e.g. user_id to be sure its the right user, just in case

$p_contact = Contact::updateOrCreate(
			[
				'id' => $contact_id,
				'user_id' =>  Auth::id()
			],
            [
                'company_name' => $request->producer['company_name'],
				...
			]
MichalOravec's avatar

Why not a simple classic update?

Contact::findOrFail($contact_id)->update([
    'company_name' => $request->producer['company_name'],
    'address' => $request->producer['address'],
    'city' => $request->producer['city'],
    'state' => $request->producer['state'],
    'zip' => $request->producer['zip'],
    'phone' => $request->producer['phone'],
    'fax' => $request->producer['fax'],
    'type' => 'producer'
]);
tykus's avatar

A classic update will not create if there is a missing Model record

1 like

Please or to participate in this conversation.