spatie / laravel-enum

Laravel support for spatie/enum
https://spatie.be/open-source
MIT License
341 stars 37 forks source link

Enum collection support for`set` column type. #69

Closed dpetrovaliev closed 3 years ago

dpetrovaliev commented 3 years ago

Hi, I have the following case:

I have a table with a column type set which is comma-separated values ex. "MALE,FEMALE". Would be great if there is an additional cast for a column with that column type.

Gummibeer commented 3 years ago

Hey,

have you tried extending the EnumCollectionCast? https://github.com/spatie/laravel-enum/blob/master/src/Casts/EnumCollectionCast.php

As a PR I think that adding a config to the options would be a good solution. Like the nullable flag - the user could provide json or comma. 🤔 I think that set would be an irritating and misleading and too specific naming, that's why I think that comma is fine. The default would be json.

dpetrovaliev commented 3 years ago

Hey, @Gummibeer

I've tried your suggestion and it works but looking at the definition it doesn't make any sense to me personally:

    protected $casts = [
        'set_of_enums' => StatusEnum::class.':collection,comma',
        'nullable_set_of_enums' => StatusEnum::class.':collection,comma,nullable',
    ];

Why don't we try to define it as a new type data type? For example:

'set_of_enums' => StatusEnum::class.':set_collection'