Closed MrAtiebatie closed 2 years ago
The suggested changes going to cause breaking change to BackedEnum
casted attribute. That's not acceptable.
i just convert the Enum to a collection for nova with this trait:
<?php
namespace RhysLees\EnumTraits\Traits;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
trait HasOptions
{
public static function options(): Collection
{
return Collection::make(self::cases())->mapWithKeys(
fn ($case) => [$case->value => Str::of($case->value)->replace('_', ' ')->value()]
);
}
Not sure if that helps but it fixed the Illegal offset for me.
<?php
namespace App\Enums;
use RhysLees\EnumTraits\Traits\HasOptions;
enum RoleEnum: string
{
use HasOptions;
case ARRANGER = 'Arranger';
case COMPOSER = 'Composer';
case LYRICIST = 'Lyricist';
}
Then i loop over each Enum and filter the relation to build the results:
class ChartsPerRolePartitionMetric extends Partition
{
public function calculate(NovaRequest $request)
{
$person = Person::find($request->resourceId);
$results = [];
foreach (RoleEnum::options() as $role) {
$results[$role] = $person->charts->filter(function ($chart) use ($role) {
return $chart->pivot->role->value === $role;
})->count();
}
return $this->result($results);
}
I believe this changes should be enough.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Description:
I'm using the great spatie/laravel-model-states package. This will cast a model's string attribute to a State class. However, this produces a bug in Nova when creating a metric. For example, when working with a column invoice_status that will be casted to a State.
With the following metric:
However, Nova tries to map the column 'invoice_status' to an array with the count result. But since invoice_status is mapped to a State class it throws an 'Illegal offset error'. It's produced in
Laravel\Nova\Metrics\Partition
in this function:Changing it to use raw original attributes will fix this:
Detailed steps to reproduce the issue on a fresh Nova installation: