Home > Enterprise >  Laravel Eloquent updateOrCreate doesn't work properly
Laravel Eloquent updateOrCreate doesn't work properly

Time:01-13

I once wrote probably same question last time and I'm back..

Laravel Eloquent firstOrCreate doesn't work properly

On the last question, I found that fillable property filters update field manifest. So, if you want to update a table based on fieldA and fieldB, then your code might be..

$modelOrRelation->updateOrCreate(
    ['fieldA' => 'a', 'fieldB' => 'b'], ['otherfields' => 'update value']
);

and you MUST specify those fields on fillable property. $fillable = ['fieldA', 'fieldB', ...]

This is what I know about firstOrCreate and updateOrCreate.

At this time, following code generate many same rows. It looks like, the first parameter ['candle_date_time_kst'] do nothing..

// candleRelation is hasMany relation..
$candleRelation = $market->candles($period);

$created = $created->add($candleRelation->updateOrCreate(
    [
        'candle_date_time_kst' => $time,
    ],
    $item
));

This creates many same candle_date_time_kst value rows. At this time, fillable property already filled target fields.

What else do I miss?

Is updateOrCreate should not trust? I didn't think so.. There are something I miss... any insight?

CodePudding user response:

you need to supply an array in the format [ column => value, ... ] not [ value ]

CodePudding user response:

I had a similar problem a time ago. And the UpdateOrInsert method solved it. Unfortunately, this method is Query Builder, not eloquent. But to achieve this result that was the only really working solution to me.

The issue for only happened when I tried to use more than 1 column on where clause, like in your example.

  •  Tags:  
  • Related