Skip to content

Commit 131dac4

Browse files
committed
[Update] Added withoutEvents boolean for updateWhere and updateOrCreate methods
1 parent ad0696e commit 131dac4

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/Contracts/RepositoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public function insert(array $attributes): ?bool;
4949

5050
public function update(array $attributes, int $id, bool $withoutEvents = false): mixed;
5151

52-
public function updateWhere(array $where, array $attributes): ?bool;
52+
public function updateWhere(array $where, array $attributes, bool $withoutEvents = false): ?bool;
5353

54-
public function updateOrCreate(array $attributes, array $values = []): mixed;
54+
public function updateOrCreate(array $attributes, array $values = [], bool $withoutEvents = false): mixed;
5555

5656
public function upsert(array $values, array|string $uniqueBy, array|null $update = null): int;
5757

src/Eloquent/BaseRepository.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,13 @@ public function update(array $attributes, int $id, bool $withoutEvents = false):
310310
return $model;
311311
}
312312

313-
public function updateWhere(array $where, array $attributes): ?bool
313+
public function updateWhere(array $where, array $attributes, bool $withoutEvents = false): ?bool
314314
{
315315
$this->applyScope();
316316
$this->applyConditions($where);
317317

318-
$updated = $this->model->update($attributes);
318+
$method = $withoutEvents ? 'updateQuietly' : 'update';
319+
$updated = $this->model->{$method}($attributes);
319320

320321
event(new RepositoryEntityUpdated($this, $this->model->getModel()));
321322

@@ -324,11 +325,12 @@ public function updateWhere(array $where, array $attributes): ?bool
324325
return $updated;
325326
}
326327

327-
public function updateOrCreate(array $attributes, array $values = []): mixed
328+
public function updateOrCreate(array $attributes, array $values = [], bool $withoutEvents = false): mixed
328329
{
329330
$this->applyScope();
330331

331-
$model = $this->model->updateOrCreate($attributes, $values);
332+
$method = $withoutEvents ? 'saveQuietly' : 'save';
333+
$model = tap($this->model->firstOrNew($attributes), fn ($instance) => $instance->fill($values)->{$method}());
332334
$this->resetModel();
333335

334336
event(new RepositoryEntityUpdated($this, $model));

0 commit comments

Comments
 (0)