For me the best approach is to have a separate trait for enums where just one toArray() method is implemented. You can then use this trait in any enum you wish to be able to be treated as an array.
trait Arrayable /*(or something else)*/
{
public static function toArray(): array
{
return array_map(
fn ($item) => $item->value,
self::cases()
);
// or
return array_column(self::cases(), 'value');
}
}
@MikhArt WOW why i can't think that, that's very cool bro
for you, which one more better
return array_column(self::cases(), 'value'); // PHP native
// or
return collect(self::cases())->pluck('value'); // can use Laravel all collection function