How to compare and update an Eloquent Collection based on another Collection in Laravel

I have a model in Laravel called Player. This player data is pulled from an external API. I am trying to update this external data on a scheduled basis from the external API. The external API data is the authoritative source on what should be in the players table.

What I currently have is two collections, one is the data from the database, the other is the external API data. I constructed new Player models in a collection based on the API data.

What I essentially have right now is this:

Collection $playersInDatabase; // Eloquent Collection of type Player
Collection $playersFromApi; // Collection of type Player

The $playersFromApi data is just the JSON API data converted into new Player models and added to the collection.

My problem is that I can’t just wipe the whole players table as I am only modifying a subset of the table at a time. Is there an effective way to use Laravel to compare these two? I want to add any new Player models to the database that don’t exist, update any existing Player model that has differing data, and then also delete any records that the API data no longer has but are still in the database (stale records).

The only way I can think of to do this involves iterating over the collections multiple times to accomplish what I want to do and I feel like there is an easier more elegant way to do this that better utilizing the framework.

For reference here is what the players table looks like. I am currently using seeder data:

example table

Source: Laravel StackOverflow

Leave a Reply