Skip to content

Commit 3646b51

Browse files
AJenbotaylorotwell
andauthored
Allow for relation key to be an enum (#50311)
* 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 <[email protected]>
1 parent 698c74b commit 3646b51

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Database\Eloquent\Relations\Concerns;
44

5+
use BackedEnum;
56
use Illuminate\Database\Eloquent\Collection;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\Pivot;
@@ -154,6 +155,10 @@ protected function formatRecordsList(array $records)
154155
[$id, $attributes] = [$attributes, []];
155156
}
156157

158+
if ($id instanceof BackedEnum) {
159+
$id = $id->value;
160+
}
161+
157162
return [$id => $attributes];
158163
})->all();
159164
}

0 commit comments

Comments
 (0)