1 | <?php |
||||
2 | |||||
3 | namespace Signifly\PivotEvents; |
||||
4 | |||||
5 | use Illuminate\Database\Eloquent\Builder; |
||||
6 | use Illuminate\Database\Eloquent\Model; |
||||
7 | use Illuminate\Support\Collection; |
||||
8 | |||||
9 | trait HasPivotEvents |
||||
10 | { |
||||
11 | protected $pivotChanges = []; |
||||
12 | |||||
13 | public function setPivotChanges(string $type, string $relation, array $ids = []): void |
||||
14 | { |
||||
15 | collect($ids)->each(function ($attributes, $id) use ($type, $relation) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
16 | data_set($this->pivotChanges, "{$type}.{$relation}.{$id}", $attributes); |
||||
17 | }); |
||||
18 | } |
||||
19 | |||||
20 | public function getPivotChanges($type = null): Collection |
||||
21 | { |
||||
22 | return $type |
||||
23 | ? collect(data_get($this->pivotChanges, $type)) |
||||
24 | : collect($this->pivotChanges); |
||||
0 ignored issues
–
show
$this->pivotChanges of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
25 | } |
||||
26 | |||||
27 | public function getPivotChangeIds($type, $relation): Collection |
||||
28 | { |
||||
29 | return collect($this->getPivotChanges("{$type}.{$relation}"))->keys(); |
||||
30 | } |
||||
31 | |||||
32 | public function resetPivotChanges(): void |
||||
33 | { |
||||
34 | $this->pivotChanges = []; |
||||
35 | } |
||||
36 | |||||
37 | public static function pivotAttaching($callback) |
||||
38 | { |
||||
39 | static::registerModelEvent('pivotAttaching', $callback); |
||||
40 | } |
||||
41 | |||||
42 | public static function pivotAttached($callback) |
||||
43 | { |
||||
44 | static::registerModelEvent('pivotAttached', $callback); |
||||
45 | } |
||||
46 | |||||
47 | public static function pivotDetaching($callback) |
||||
48 | { |
||||
49 | static::registerModelEvent('pivotDetaching', $callback); |
||||
50 | } |
||||
51 | |||||
52 | public static function pivotDetached($callback) |
||||
53 | { |
||||
54 | static::registerModelEvent('pivotDetached', $callback); |
||||
55 | } |
||||
56 | |||||
57 | public static function pivotUpdating($callback) |
||||
58 | { |
||||
59 | static::registerModelEvent('pivotUpdating', $callback); |
||||
60 | } |
||||
61 | |||||
62 | public static function pivotUpdated($callback) |
||||
63 | { |
||||
64 | static::registerModelEvent('pivotUpdated', $callback); |
||||
65 | } |
||||
66 | |||||
67 | public function firePivotAttachingEvent($halt = true) |
||||
68 | { |
||||
69 | return $this->fireModelEvent('pivotAttaching', $halt); |
||||
0 ignored issues
–
show
It seems like
fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
70 | } |
||||
71 | |||||
72 | public function firePivotAttachedEvent($halt = false) |
||||
73 | { |
||||
74 | return $this->fireModelEvent('pivotAttached', $halt); |
||||
75 | } |
||||
76 | |||||
77 | public function firePivotDetachingEvent($halt = true) |
||||
78 | { |
||||
79 | return $this->fireModelEvent('pivotDetaching', $halt); |
||||
80 | } |
||||
81 | |||||
82 | public function firePivotDetachedEvent($halt = false) |
||||
83 | { |
||||
84 | return $this->fireModelEvent('pivotDetached', $halt); |
||||
85 | } |
||||
86 | |||||
87 | public function firePivotUpdatingEvent($halt = true) |
||||
88 | { |
||||
89 | return $this->fireModelEvent('pivotUpdating', $halt); |
||||
90 | } |
||||
91 | |||||
92 | public function firePivotUpdatedEvent($halt = false) |
||||
93 | { |
||||
94 | return $this->fireModelEvent('pivotUpdated', $halt); |
||||
95 | } |
||||
96 | |||||
97 | /** |
||||
98 | * Get the observable event names. |
||||
99 | * |
||||
100 | * @return array |
||||
101 | */ |
||||
102 | public function getObservableEvents() |
||||
103 | { |
||||
104 | return array_merge( |
||||
105 | parent::getObservableEvents(), |
||||
106 | [ |
||||
107 | 'pivotAttaching', 'pivotAttached', |
||||
108 | 'pivotDetaching', 'pivotDetached', |
||||
109 | 'pivotUpdating', 'pivotUpdated', |
||||
110 | ] |
||||
111 | ); |
||||
112 | } |
||||
113 | |||||
114 | /** |
||||
115 | * Instantiate a new BelongsToMany relationship. |
||||
116 | * |
||||
117 | * @param \Illuminate\Database\Eloquent\Builder $query |
||||
118 | * @param \Illuminate\Database\Eloquent\Model $parent |
||||
119 | * @param string $table |
||||
120 | * @param string $foreignPivotKey |
||||
121 | * @param string $relatedPivotKey |
||||
122 | * @param string $parentKey |
||||
123 | * @param string $relatedKey |
||||
124 | * @param string $relationName |
||||
125 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||||
126 | */ |
||||
127 | protected function newBelongsToMany( |
||||
128 | Builder $query, |
||||
129 | Model $parent, |
||||
130 | $table, |
||||
131 | $foreignPivotKey, |
||||
132 | $relatedPivotKey, |
||||
133 | $parentKey, |
||||
134 | $relatedKey, |
||||
135 | $relationName = null |
||||
136 | ) { |
||||
137 | return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); |
||||
138 | } |
||||
139 | } |
||||
140 |