Pedro
Pedro

Reputation: 1477

UpdateOrCreate not working has expected

im trying to user the method "updateOrCreate", and works almost has expected but with a problem.

For example if the record doesnt exists it creates, untill now great, but for example if i change a value in my case the "mybankroll" value it creates another record. I can that understand that is creating because one of the columns record value doesnt exist, but i dont understand how the update actions is fired.

My code example:

UserBankroll::updateOrCreate(
            ['my_bankroll' => $request->mybankroll, 'currency' => 'EUR'],
            ['user_id' => Auth::user()->id]
        );

But if i try to update the value of my_bankroll, instead of updating it created another record.

Upvotes: 0

Views: 2639

Answers (2)

Vasim Shaikh
Vasim Shaikh

Reputation: 4542

UserBankroll::updateOrCreate(
        ['user_id' => Auth::user()->id],
            ['my_bankroll' => $request->mybankroll, 'currency' => 'EUR']

        );

Upvotes: 2

Devon Bessemer
Devon Bessemer

Reputation: 35367

You seem to have your arrays backwards.

The first array should be what you are matching against, the second array should be the update values.

https://laravel.com/api/5.0/Illuminate/Database/Eloquent/Model.html#method_updateOrCreate

Upvotes: 4

Related Questions