Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
Created June 24, 2018 03:52
Show Gist options
  • Select an option

  • Save jarektkaczyk/3eaff4ccd1966183c58d18d85def293f to your computer and use it in GitHub Desktop.

Select an option

Save jarektkaczyk/3eaff4ccd1966183c58d18d85def293f to your computer and use it in GitHub Desktop.

Revisions

  1. jarektkaczyk revised this gist Jun 24, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions check-children.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    $ids = array_unique([1,2,3,4,5, ...]);

    // relation() is HasMany
  2. jarektkaczyk created this gist Jun 24, 2018.
    14 changes: 14 additions & 0 deletions check-children.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    $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);