@@ -62,23 +62,103 @@ public function restored(Model $model)
6262 $ this ->invalidateCache ($ model );
6363 }
6464
65+ /**
66+ * Invalidate attach for belongsToMany.
67+ *
68+ * @param string $relation
69+ * @param \Illuminate\Database\Eloquent\Model $model
70+ * @param array $ids
71+ * @return void
72+ */
73+ public function belongsToManyAttached ($ relation , Model $ model , $ ids )
74+ {
75+ $ this ->invalidateCache ($ model , $ relation , $ model ->{$ relation }()->findMany ($ ids ));
76+ }
77+
78+ /**
79+ * Invalidate detach for belongsToMany.
80+ *
81+ * @param string $relation
82+ * @param \Illuminate\Database\Eloquent\Model $model
83+ * @param array $ids
84+ * @return void
85+ */
86+ public function belongsToManyDetached ($ relation , Model $ model , $ ids )
87+ {
88+ $ this ->invalidateCache ($ model , $ relation , $ model ->{$ relation }()->findMany ($ ids ));
89+ }
90+
91+ /**
92+ * Invalidate update pivot for belongsToMany.
93+ *
94+ * @param string $relation
95+ * @param \Illuminate\Database\Eloquent\Model $model
96+ * @param array $ids
97+ * @return void
98+ */
99+ public function belongsToManyUpdatedExistingPivot ($ relation , Model $ model , $ ids )
100+ {
101+ $ this ->invalidateCache ($ model , $ relation , $ model ->{$ relation }()->findMany ($ ids ));
102+ }
103+
104+ /**
105+ * Invalidate attach for morphToMany.
106+ *
107+ * @param string $relation
108+ * @param \Illuminate\Database\Eloquent\Model $model
109+ * @param array $ids
110+ * @return void
111+ */
112+ public function morphToManyAttached ($ relation , Model $ model , $ ids )
113+ {
114+ $ this ->invalidateCache ($ model , $ relation , $ model ->{$ relation }()->findMany ($ ids ));
115+ }
116+
117+ /**
118+ * Invalidate detach for morphToMany.
119+ *
120+ * @param string $relation
121+ * @param \Illuminate\Database\Eloquent\Model $model
122+ * @param array $ids
123+ * @return void
124+ */
125+ public function morphToManyDetached ($ relation , Model $ model , $ ids )
126+ {
127+ $ this ->invalidateCache ($ model , $ relation , $ model ->{$ relation }()->findMany ($ ids ));
128+ }
129+
130+ /**
131+ * Invalidate update pivot for morphToMany.
132+ *
133+ * @param string $relation
134+ * @param \Illuminate\Database\Eloquent\Model $model
135+ * @param array $ids
136+ * @return void
137+ */
138+ public function morphToManyUpdatedExistingPivot ($ relation , Model $ model , $ ids )
139+ {
140+ $ this ->invalidateCache ($ model , $ relation , $ model ->{$ relation }()->findMany ($ ids ));
141+ }
142+
65143 /**
66144 * Invalidate the cache for a model.
67145 *
68146 * @param \Illuminate\Database\Eloquent\Model $model
147+ * @param string|null $relation
148+ * @param \Illuminate\Database\Eloquent\Collection|null $pivotedModels
69149 * @return void
70150 * @throws Exception
71151 */
72- protected function invalidateCache (Model $ model ): void
152+ protected function invalidateCache (Model $ model, $ relation = null , $ pivotedModels = null ): void
73153 {
74154 $ class = get_class ($ model );
75155
76- if (! $ model ->getCacheTagsToInvalidateOnUpdate ()) {
156+ if (! $ model ->getCacheTagsToInvalidateOnUpdate ($ relation , $ pivotedModels )) {
77157 throw new Exception ('Automatic invalidation for ' .$ class .' works only if at least one tag to be invalidated is specified. ' );
78158 }
79159
80160 $ class ::flushQueryCache (
81- $ model ->getCacheTagsToInvalidateOnUpdate ()
161+ $ model ->getCacheTagsToInvalidateOnUpdate ($ relation , $ pivotedModels )
82162 );
83163 }
84164}
0 commit comments