Skip to content

Commit f034566

Browse files
committed
fix: filling attributes in perform* methods overrides values set in hooks
1 parent 87337d2 commit f034566

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Concerns/HandlesRelationStandardOperations.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Database\Eloquent\Relations\MorphToMany;
1515
use Illuminate\Database\Eloquent\Relations\Relation;
1616
use Illuminate\Database\Eloquent\SoftDeletes;
17+
use Illuminate\Support\Arr;
1718
use InvalidArgumentException;
1819
use Orion\Http\Requests\Request;
1920
use Orion\Http\Resources\CollectionResource;
@@ -215,7 +216,9 @@ protected function runStoreParentFetchQuery(Request $request, Builder $query, $p
215216
*/
216217
protected function performStore(Request $request, Model $parentEntity, Model $entity, array $attributes, array $pivot): void
217218
{
218-
$entity->fill($attributes);
219+
$entity->fill(
220+
Arr::except($attributes, array_keys($entity->getDirty()))
221+
);
219222

220223
if (!$parentEntity->{$this->getRelation()}() instanceof BelongsTo) {
221224
$parentEntity->{$this->getRelation()}()->save($entity, $this->preparePivotFields($pivot));
@@ -442,7 +445,9 @@ protected function runUpdateFetchQuery(Request $request, Relation $query, Model
442445
*/
443446
protected function performUpdate(Request $request, Model $parentEntity, Model $entity, array $attributes, array $pivot): void
444447
{
445-
$entity->fill($attributes);
448+
$entity->fill(
449+
Arr::except($attributes, array_keys($entity->getDirty()))
450+
);
446451
$entity->save();
447452

448453
$relation = $parentEntity->{$this->getRelation()}();

src/Concerns/HandlesStandardOperations.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Collection;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\SoftDeletes;
11+
use Illuminate\Support\Arr;
1112
use Orion\Http\Requests\Request;
1213
use Orion\Http\Resources\CollectionResource;
1314
use Orion\Http\Resources\Resource;
@@ -133,7 +134,9 @@ public function store(Request $request)
133134
*/
134135
protected function performStore(Request $request, Model $entity, array $attributes): void
135136
{
136-
$entity->fill($attributes);
137+
$entity->fill(
138+
Arr::except($attributes, array_keys($entity->getDirty()))
139+
);
137140
$entity->save();
138141
}
139142

@@ -275,7 +278,9 @@ protected function runUpdateFetchQuery(Request $request, Builder $query, $key):
275278
*/
276279
protected function performUpdate(Request $request, Model $entity, array $attributes): void
277280
{
278-
$entity->fill($attributes);
281+
$entity->fill(
282+
Arr::except($attributes, array_keys($entity->getDirty()))
283+
);
279284
$entity->save();
280285
}
281286

0 commit comments

Comments
 (0)