Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add support for BackedEnum in Collection groupBy method #47823

Merged
merged 2 commits into from Jul 24, 2023

Conversation

osbre
Copy link
Contributor

@osbre osbre commented Jul 24, 2023

I tried to group the Eloquent model collection by the Enum-casted key and got this error:

TypeError
array_key_exists(): Argument #1 ($key) must be a valid array offset type

This PR aims to fix that by adding support for BackedEnum in groupBy method.

Code example

$attributesByCategory = Attribute::whereIn('category', [
      AttributeCategory::LOREM,
      AttributeCategory::IPSUM,
  ])
  ->get()
  ->groupBy('category');

enum AttributeCategory: string
{
    case LOREM  = 'lorem';
    case IPSUM   = 'ipsum';
}

class Attribute extends Model
{
    protected $casts = [
        'category' => AttributeCategory::class,
    ];
}

@taylorotwell taylorotwell merged commit 971e822 into laravel:10.x Jul 24, 2023
14 checks passed
@osbre osbre mentioned this pull request Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants