staticcode's avatar

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?

0 likes
8 replies
samalapsy's avatar

@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.

tykus's avatar

Is the $items['sku_id'] exactly the same; have you trimmed any leading/trailing spaces?

Note: you do not need to have the same key/value pair(s) (i.e. sku_id) that locates the record in the (new) attributes list as well.

staticcode's avatar

I checked the outcome and the sku_id is the exact same as the one already in the database. There is literally zero difference between the two.

And thanks @tykus for pointing that out, didnt know :D

I tried updateOrCreate instead of firstOrCreate but when using updateOrCreate it doesnt add anything at all to the database.

samalapsy's avatar

@staticcode Can you try to Log out the content of your 'items Array' while performing the firstOrCreate function? and compare

tykus's avatar

firstOrCreate and updateOrCreate are not the same exactly; so you would need to decide exactly which matches your needs:

  • updateOrCreate - when a found record, it would be updated with the new attribute values
  • firstOrCreate - when a found record, any new attribute values would be ignored

Can you dump Products::where(['sku_id' => $items['sku_id']])->first() prior to your firstOrCreate call - just to see if the existing record is found?

1 like
staticcode's avatar

@samalapsy yeah did that:

echo '<pre>';
                            print_r( $items['sku_id'] );
                            echo '</pre>';

                            echo '<pre>';
                            print_r( $items['fk_id'] );
                            echo '</pre>';

                            // update or add products
                            $query = Products::updateOrCreate(['sku_id' => $items['sku_id']], [
                                'sku_id' => $items['sku_id'],
                                'fk_id' => $items['fk_id'],

strange thing is... before the update it shows me the correct values "12354" but in the database its empty. Eloquent simply does not add anything to sku_id and fk_id.

Its not a database issue, cause I can manually add them and everything is alright.

tykus's avatar

Are those attributes mass-assignable on the model; because they will be empty in the database otherwise?

1 like

Please or to participate in this conversation.