Skip to content

[Proposal] Diffing of Eloquent collections #2780

@dwightwatson

Description

@dwightwatson

i would like to propose the inclusion of diffing of Eloquent collections. This feature could be useful in belongsToMany relationships, for example if a blog Post belonged to many Categories, you could find the categories it doesn't belong to by diffing the collections.

My proposed implementation is below and I have a branch ready with tests to make a pull request if this is accepted.

public function diff(Collection $collection)
{
    $diff = new static;

    foreach ($this->items as $item)
    {
        if ( ! $collection->contains($item->getKey()))
        {
            $diff->add($item);
        }
    }

    return $diff;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions