2

How to implement updateOrCreate function with where clause.

For example. I would want to update a field only if certain column contains a specific value otherwise don't update. Is it possible with updateOrCreate function?

5
  • I would want to update a field only if certain column contains a specific value otherwise don't update. you are saying you want to update if some criteria matches otherwise don't update. while the updateOrCreate method is for updating a row if exist otherwise create a new row. what actually you want?? Commented Nov 27, 2019 at 10:32
  • I just want to know whether it is possible to check some criteria before updating. Commented Nov 27, 2019 at 11:08
  • if you just want to update if criteria matches and don't create a new row if not matches then use where with update. Commented Nov 27, 2019 at 11:14
  • 1
    See there are two cases - I need to create a new row if key does not exists. and if key already exists then check if value matches certain criteria , if yes then update or else don't update. Commented Nov 27, 2019 at 11:47
  • then you have to do it manually. updateOrCreate method accepts two array parameter. the first array contains key value to check in the database and the second one contains value to be inserted or updated. Commented Nov 27, 2019 at 12:06

4 Answers 4

10

updateOrCreate is an update with where-clause:

$user = User::updateOrCreate(
    [
        'id'    => $userId,
        'type'  => $userType,
    ], 
    [
        'name'  => $userName,
        'email' => $userEmail,
    ]
);

This query will update the user with $userName and $userEmail if a user exists with matching $userId and $userType. If no matching user is found, a new user is created with $userId, $userType, $userName and $userEmail.

So the first array is the "where-clause" that has to match for the update, second array is the values that should be updated if a match is found and a merge of both arrays are used when creating a new user if no match was found.

See docs here

Sign up to request clarification or add additional context in comments.

Comments

2

updateOrCreate mainly used when you upadate one column value in your model.You don't know the condition either this row is exists or not.If the row is exists then it just upadte your column value otherwise it create that row.

$update_value = YourModel::updateOrCreate(
    ['your_checking_column' => 'your_value',],
    ['your_updated_coumn' => 'your_updated_value']
);

Comments

1

updateOrCreate doesn't provide this functionality as far as I know. Instead you can use regular where clause followed by update. From your question I see that you don't need to create at all.

Something::where('column', 'value')->first()->update(['otherColumn' => 'some value']);

1 Comment

I am using create so that if a new entry is made for primary key it will create an entry in the table or else it will update the corresponding entry of that primary field.
0

If you're trying to set a value based on some criteria you can use a ternary:

'user_id' => ($request->user_id && is_numeric($request->user_id) ? $request->user_id : \Auth::user()->id)

This is the equivalent of saying, if user_id is provided and numeric, set the value to $request->user_id, otherwise take the user_id from the authenticated user, a simpler example:

'user_id' => ($request->user_id ? $request->user_id : null)

If a user_id is give in the request use that value, otherwise set value to null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.