Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
This repository was archived by the owner on Jul 16, 2021. It is now read-only.

Casting a null value to a collection should return an empty collection #1278

@hackel

Description

@hackel

When casting an Eloquent model attribute to a 'collection', if the value is currently null, the cast does not take place, and null is returned.

To avoid having to litter the code with existence tests for that attribute, and also be consistent with relationships, I propose an empty collection be returned instead.

On the saving side, there could also be logic to save an empty collection as null, but this is not critical.

Unfortunately this would be a breaking change. Any existence check e.g. if ($model->collection) would need to be changed to if ($model->collection->isEmpty()), but in many of those cases, I suspect the check would no longer be necessary.

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