Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
Created June 24, 2018 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarektkaczyk/3eaff4ccd1966183c58d18d85def293f to your computer and use it in GitHub Desktop.
Save jarektkaczyk/3eaff4ccd1966183c58d18d85def293f to your computer and use it in GitHub Desktop.
<?php
$ids = array_unique([1,2,3,4,5, ...]);
// relation() is HasMany
// check if all $ids belong to this parent model:
$parent_model->relation()->whereIn('id', $ids)->count() === count($ids)
// check if $ids match all of the children records:
$parent_model->relation()->count() === count($ids)
// if necessary check what's missing and where:
$ids = collect($ids);
$related_ids = $parent_model->relation()->pluck('id');
$missing_input = $related_ids->diff($ids);
$missing_in_db = $ids->diff($related_ids);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment