Skip to content

Commit 29c60f4

Browse files
committed
add withoutEvents flag for create & update method
1 parent 6edb1b9 commit 29c60f4

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/Contracts/RepositoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function findWhereNotIn($field, array $values, $columns = ['*']): mixed;
4343

4444
public function findWhereBetween($field, array $values, $columns = ['*']): mixed;
4545

46-
public function create(array $attributes): mixed;
46+
public function create(array $attributes, bool $withoutEvents = false): mixed;
4747

48-
public function update(array $attributes, int $id): mixed;
48+
public function update(array $attributes, int $id, bool $withoutEvents = false): mixed;
4949

5050
public function updateOrCreate(array $attributes, array $values = []): mixed;
5151

src/Eloquent/BaseRepository.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,25 +266,31 @@ public function findWhereBetween($field, array $values, $columns = ['*']): mixed
266266
return $model;
267267
}
268268

269-
public function create(array $attributes): mixed
269+
public function create(array $attributes, bool $withoutEvents = false): mixed
270270
{
271271
$model = $this->model->newInstance($attributes);
272-
$model->save();
272+
273+
$method = $withoutEvents ? 'saveQuietly' : 'save';
274+
$model->{$method}();
275+
273276
$this->resetModel();
274277

275278
event(new RepositoryEntityCreated($this, $model));
276279

277280
return $model;
278281
}
279282

280-
public function update(array $attributes, int $id): mixed
283+
public function update(array $attributes, int $id, bool $withoutEvents = false): mixed
281284
{
282285
$this->applyScope();
283286

284287
$model = $this->model->findOrFail($id);
285288

286289
$model->fill($attributes);
287-
$model->save();
290+
291+
$method = $withoutEvents ? 'saveQuietly' : 'save';
292+
$model->{$method}();
293+
288294
$this->resetModel();
289295

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

0 commit comments

Comments
 (0)