Skip to content
Advertisement

Laravel – Delete record from database if not exists in given array

I am fetching all my GitHub repositories using Campbells GitHub package for Laravel. I made a command and a cronjob which runs every minute and it works perfectly fine. The only problem is that when a repository gets deleted from GitHub it stays in my database instead of also getting deleted.

I came across a method called whereNotIn() which checks if the data does not meet the specified input and you can do actions to it accordingly. But I can’t seem to make it work.

Say for instance this bottom table is a list of GitHub repos and my repos in my database

JavaScript

You see that the list of GitHub repos is one short of the website repos, which means a repo got deleted on the GitHub side so in that case Repo 5 also needs to get deleted to stay up-to-date with the changes.

Handle method within the command

JavaScript

Advertisement

Answer

You can do it as following:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement