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.

Eloquent casts to array return an empty array by default #369

@alexdover

Description

@alexdover

I've just been using the casts feature to cast a text field to an array, very helpful.

However, when the text field is empty the default return value is null. At present the default behaviour is to return the result of json_decode. I would propose an extra check here for the result and return an empty array instead.

This would then avoid having to check if the attribute is an array prior to handling as such.

For example

if (in_array(1, $my_model->options) {
    // ...
}

rather than

if (is_array($my_model->options) && in_array(1, $my_model->options) {
    // ...
}

I'd be happy to put this change together as a pull request but wanted to see if anyone had any reasons why this shouldn't be the default behaviour?

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