exists(); }); $subtasks = array_filter($events, function ($event) { $projectCode = explode(' ', $event['subject'])[0]; // if project code exist in database, then it is subtask return Project::where('code', $projectCode)->exists(); }); // get only new events - the ones that doesnt have any office365_id in blockers table and only events that doesnt have isCancelled => true $newBlockers = array_filter($blockers_from_office365, function ($event) { return ! Blocker::where('office365_id', $event['id'])->exists() && ! $event['isCancelled']; }); $newSubtasks = array_filter($subtasks, function ($event) { return ! SubTask::where('office365_id', $event['id'])->exists() && ! $event['isCancelled']; }); Office365Service::addBlockers($newBlockers); Office365Service::addSubtasks($newSubtasks); // get edited blockers - the ones, that has different changeKey from our database $editedBlockers = array_filter($blockers_from_office365, function ($event) { $blocker = Blocker::where('office365_id', $event['id'])->first(); return $blocker && $blocker->office365_change_key !== $event['changeKey']; }); Office365Service::editBlockers($editedBlockers); // get all canceled blockers - the ones, that are in our database and in the response they have isCancelled => true $removedBlockers = Blocker::whereNotNull('office365_id')->get()->filter(function ($blocker) use ( $blockers_from_office365 ) { $event = collect($blockers_from_office365)->firstWhere('id', $blocker->office365_id); return $event && $event['isCancelled']; }); // TODO dat do service $removedBlockers->each(function ($blocker) { $blocker->delete(); }); dd('he'); } }