Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wasimrasheed's avatar

Import Excel Sheet and Sync the data in database

I am trying to import an excel sheet which have some new and some old employee records and I want to do this it should create the new record for the new employees data and should delete the data of employees which is not present in the excell sheet.

0 likes
2 replies
click's avatar

What did you try so far?

For reading the XLS you can use one of the following packages:

The last one has an upsert strategy build in explained in the docs: https://docs.laravel-excel.com/3.1/imports/model.html#upserting-models

If you don't want to use the laravel-excel.com package you can handle upserting (update or insert) your self. Laravel has a build in method for this see: https://laravel.com/docs/9.x/eloquent#upserts

The only thing left is to delete records that are not available in your XLS. The simplest solution would be to keep track of all the ids that you inserted or updated in your XLS and at the end run a query: DELETE FROM your_table where id NOT IN (1,2,3,4). Where 1, 2, 3 and 4 are the ids that were updated or inserted.

But be careful with the 'delete', if you or someone uploaded an xls that is not correct you might delete all of your records by accident.

Alternatively you could add an last_updated_at column to your database and update it each time the XLS is processed. And then if you are OK with everything you could say: delete all records which were not updated in the last 24 hours.

1 like
Sinnbeck's avatar

Be sure to check of anyone uploads an empty excel sheet. If they do, it should delete every single record in the database, according to your description :)

Please or to participate in this conversation.