@staticcode I think there are some data that are changing, Maybe the sku_id is changing. If the sku_id is different from what's in the DB it'll insert into the DB.
Mar 13, 2018
8
Level 1
firstOrCreate not working properly
Hi guys,
I recently made an import script by using Laravel's firstOrCreate function which, to my understanding, should update if non-existant and other create the given entry. See my code below:
$query = Products::firstOrCreate(['sku_id' => $items['sku_id']], [
'sku_id' => $items['sku_id'],
'fk_id' => $items['fk_id'],
'status' => $items['status'],
'brand' => $items['brand'],
'article_number' => $items['article_number'],
'color' => $items['color'],
'size' => $items['size'],
'article_name' => $items['article_name'],
'description' => preg_replace("/[^a-zA-Z0-9%\/\s]/", "", preg_replace('#\s{2,}#', ' ', (preg_replace('#\s#', ' ', $items['description'])))),
'material' => preg_replace("/[^a-zA-Z0-9%\/\s]/", "", $items['material']),
'category' => $items['category'],
'washing_instructions' => preg_replace("/[^a-zA-Z0-9%\/\s]/", "", $items['washing_instructions']),
'color_id' => $items['color_id'],
'photo_1' => $items['photo_1'],
'photo_2' => $items['photo_2'],
'photo_3' => $items['photo_3'],
'photo_4' => $items['photo_4'],
'photo_5' => $items['photo_5'],
'photo_main' => $items['photo_main'],
'weight' => $items['weight'],
'price' => ($items['price'] * 1)
]);
The problem that I am having is that when I import, lets say, 2 products. And I do the import again, it just simply adds all entries again and I have 4 things in my database instead of Eloquent updating the existing values.
Can anyone explain to me what I am doing wrong?
Please or to participate in this conversation.