From 26c1cf9a5331477b32188a12a36bc2d2c2980b93 Mon Sep 17 00:00:00 2001 From: Matej Date: Wed, 22 Sep 2021 01:23:31 +0200 Subject: [PATCH] Add `saveQuietly` convenience methods to relations --- .../Database/Eloquent/Relations/BelongsToMany.php | 15 +++++++++++++++ .../Database/Eloquent/Relations/HasOneOrMany.php | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 407390e209e4..a9258b585811 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -1155,6 +1155,21 @@ public function save(Model $model, array $pivotAttributes = [], $touch = true) return $model; } + /** + * Save a new model without raising any events and attach it to the parent model. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param array $pivotAttributes + * @param bool $touch + * @return \Illuminate\Database\Eloquent\Model + */ + public function saveQuietly(Model $model, array $pivotAttributes = [], $touch = true) + { + return Model::withoutEvents(function () use ($model, $pivotAttributes, $touch) { + return $this->save($model, $pivotAttributes, $touch); + }); + } + /** * Save an array of new models and attach them to the parent model. * diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 1c7ff591cd9c..4de065152d5b 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -267,6 +267,19 @@ public function save(Model $model) return $model->save() ? $model : false; } + /** + * Attach a model instance without raising any events to the parent model. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @return \Illuminate\Database\Eloquent\Model|false + */ + public function saveQuietly(Model $model) + { + return Model::withoutEvents(function () use ($model) { + return $this->save($model); + }); + } + /** * Attach a collection of models to the parent instance. *