Home > Software design >  Laravel updateOrCreate Method -- Update the same column that you're matching
Laravel updateOrCreate Method -- Update the same column that you're matching

Time:09-17

You know in updateOrCreate method we pass two parameters. The first parameter is used to check for a match and the second parameter carries the data that needs to be updated.

But in my scenario I need to update the same data that I'm passing in the 1st parameter. But I can't update it If I pass the same in the second parameter.

What could be the solution for this?

CodePudding user response:

With the updateOrCreate method, the second parameter is optional, so you can ignore it.

You might want to check the Illuminate\Database\Eloquent\Builder::updateOrCreate() method to see how that works.

Reading your comment, you can do something like this:

YourModel::updateOrCreate(
    ['your_field' => 9],
    ['your_field' => 10]
);

// Or
YourModel::where('your_field', 9)->update(['your_field' => 10]);
  • Related