@@ -124,6 +124,13 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
124
124
*/
125
125
protected static $ globalScopes = [];
126
126
127
+ /**
128
+ * The list of models classes that should not be affected with touch.
129
+ *
130
+ * @var array
131
+ */
132
+ protected static $ ignoreOnTouch = [];
133
+
127
134
/**
128
135
* The name of the "created at" column.
129
136
*
@@ -203,6 +210,51 @@ protected static function bootTraits()
203
210
}
204
211
}
205
212
213
+ /**
214
+ * Disables relationship model touching for the current class during given callback scope.
215
+ *
216
+ * @param callable $callback
217
+ */
218
+ public static function withoutTouching (callable $ callback )
219
+ {
220
+ static ::withoutTouchingOn ([static ::class], $ callback );
221
+ }
222
+
223
+ /**
224
+ * Disables relationship model touching for the given model classes during given callback scope.
225
+ *
226
+ * @param array $models
227
+ * @param callable $callback
228
+ */
229
+ public static function withoutTouchingOn (array $ models , callable $ callback )
230
+ {
231
+ static ::$ ignoreOnTouch = array_values (array_merge (static ::$ ignoreOnTouch , $ models ));
232
+
233
+ try {
234
+ call_user_func ($ callback );
235
+ } finally {
236
+ static ::$ ignoreOnTouch = array_values (array_diff (static ::$ ignoreOnTouch , $ models ));
237
+ }
238
+ }
239
+
240
+ /**
241
+ * @param string $class The class name to check. Defaults to current class.
242
+ *
243
+ * @return bool
244
+ */
245
+ public static function isIgnoringTouch ($ class = null )
246
+ {
247
+ $ class = $ class ?: static ::class;
248
+
249
+ foreach (static ::$ ignoreOnTouch as $ ignoredClass ) {
250
+ if ($ class === $ ignoredClass || is_subclass_of ($ class , $ ignoredClass )) {
251
+ return true ;
252
+ }
253
+ }
254
+
255
+ return false ;
256
+ }
257
+
206
258
/**
207
259
* Clear the list of booted models so they will be re-booted.
208
260
*
0 commit comments