Skip to content

Commit

Permalink
Allow for relation key to be an enum (#50311)
Browse files Browse the repository at this point in the history
* Allow for relation key to be an enum

If a foring key is being cast to an enum calling ->sync() will cause a TypeError: Illegal offset type.

This resolves the issue by getting the value before using it as an array key.

* Update InteractsWithPivotTable.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
AJenbo and taylorotwell authored Feb 29, 2024
1 parent 698c74b commit 3646b51
Showing 1 changed file with 5 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Eloquent\Relations\Concerns;

use BackedEnum;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
Expand Down Expand Up @@ -154,6 +155,10 @@ protected function formatRecordsList(array $records)
[$id, $attributes] = [$attributes, []];
}

if ($id instanceof BackedEnum) {
$id = $id->value;
}

return [$id => $attributes];
})->all();
}
Expand Down

0 comments on commit 3646b51

Please sign in to comment.