I'm having problem understanding the updateOrCreate Eloquent method.
I have a simple DB where I'm saving information from API and running every x minutes.
What I need to to is to do an INSERT if the row doesn't exists in the DB or otherwise just do an UPDATE and update the changed data.
How can we achieve this functionality in Laravel?
Here is what I have right now, but what happens it rewrites the same data across all rows.
Not sure if I understand it correctly.
I'm basically passing in a "condition" ie. fields that needs to be checked against if they are unique - campaign_id+variation_id should be unique. (it's backed by UNIQUE key in MySQL) and my data array:
$data = [
'campaign_id' => $variation['campaign_id'],
'variation_id' => $variation['variation_id'],
'name' => $variation['name'],
'description' => $variation['description'],
];
MyModel::updateOrCreate( ['campaign_id' => $variation['campaign_id'], 'variation_id' =>
$variation['variation_id'] ], $data);
It seems to work with the updateOrInsert which is a Query Builder method, but I wanted to achieve this with Eloquent, thought it will be easy. Plus Query Builder doesn't fill the timestamps for me.
Thanks for any hints!