Skip to content

Commit 6a818bb

Browse files
authored
Merge pull request #70 from Pierre-Adrien/add-return-false-to-prevent-changes
Update events handler to work like the eloquent ones
2 parents acd3833 + 0b467de commit 6a818bb

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Traits/FiresPivotEventsTrait.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public function attach($ids, array $attributes = [], $touch = true)
1717
{
1818
list($idsOnly, $idsAttributes) = $this->getIdsWithAttributes($ids, $attributes);
1919

20-
$this->parent->fireModelEvent('pivotAttaching', true, $this->getRelationName(), $idsOnly, $idsAttributes);
20+
if ($this->parent->fireModelEvent('pivotAttaching', true, $this->getRelationName(), $idsOnly, $idsAttributes) === false) {
21+
return false;
22+
}
23+
2124
$parentResult = parent::attach($ids, $attributes, $touch);
2225
$this->parent->fireModelEvent('pivotAttached', false, $this->getRelationName(), $idsOnly, $idsAttributes);
2326

@@ -40,7 +43,10 @@ public function detach($ids = null, $touch = true)
4043

4144
list($idsOnly) = $this->getIdsWithAttributes($ids);
4245

43-
$this->parent->fireModelEvent('pivotDetaching', true, $this->getRelationName(), $idsOnly);
46+
if ($this->parent->fireModelEvent('pivotDetaching', true, $this->getRelationName(), $idsOnly) === false) {
47+
return false;
48+
}
49+
4450
$parentResult = parent::detach($ids, $touch);
4551
$this->parent->fireModelEvent('pivotDetached', false, $this->getRelationName(), $idsOnly);
4652

@@ -59,7 +65,10 @@ public function updateExistingPivot($id, array $attributes, $touch = true)
5965
{
6066
list($idsOnly, $idsAttributes) = $this->getIdsWithAttributes($id, $attributes);
6167

62-
$this->parent->fireModelEvent('pivotUpdating', true, $this->getRelationName(), $idsOnly, $idsAttributes);
68+
if ($this->parent->fireModelEvent('pivotUpdating', true, $this->getRelationName(), $idsOnly, $idsAttributes) === false) {
69+
return false;
70+
}
71+
6372
$parentResult = parent::updateExistingPivot($id, $attributes, $touch);
6473
$this->parent->fireModelEvent('pivotUpdated', false, $this->getRelationName(), $idsOnly, $idsAttributes);
6574

0 commit comments

Comments
 (0)