Summer Sale! All accounts are 50% off this week.

young_artisan's avatar

Find the number of change rows

Hi I use updateOrCreate method for import data to DB and now I want to find the number of rows created and the number of rows updated.

0 likes
4 replies
young_artisan's avatar

@tisuchi thanks for your reply but that solution does not work for me, I have many data and in a loop inserted to db with updateOrCreate method, now I want to find the number of inserted and if exist update the data instead of creating and find the number of updated data.

kfirba's avatar

@young_artisan Hey

It may not answer your question but you may be interested in my answer for another thread where I suggest a better way to import bulks of data while inserting new rows and updating old ones: https://laracasts.com/discuss/channels/eloquent/performant-way-to-mass-update-or-create/replies/365924

As for your question, the easiest way to calculate that is to run a simple select count on your database prior to inserting/updating:

$newRows = count($values) - Model::whereIn('your_primary_key', $values)->count();
1 like

Please or to participate in this conversation.