Skip to content

Commit 1cf7324

Browse files
[12.x] Cache isSoftDeletable(), isPrunable(), and isMassPrunable() directly in model (#56078)
* Do caching of isSoftDeletable(), isPrunable(), and isMassPrunable() directly in the model * StyleCI * Change cached boolean to map with late static binding Co-authored-by: Choraimy Kroonstuiver <[email protected]> See #56078 (comment) * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 5528074 commit 1cf7324

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,27 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
250250
*/
251251
protected static string $collectionClass = Collection::class;
252252

253+
/**
254+
* Cache of soft deletable models.
255+
*
256+
* @var array<class-string<self>, bool>
257+
*/
258+
protected static array $isSoftDeletable;
259+
260+
/**
261+
* Cache of prunable models.
262+
*
263+
* @var array<class-string<self>, bool>
264+
*/
265+
protected static array $isPrunable;
266+
267+
/**
268+
* Cache of mass prunable models.
269+
*
270+
* @var array<class-string<self>, bool>
271+
*/
272+
protected static array $isMassPrunable;
273+
253274
/**
254275
* The name of the "created at" column.
255276
*
@@ -2294,23 +2315,23 @@ public function setPerPage($perPage)
22942315
*/
22952316
public static function isSoftDeletable(): bool
22962317
{
2297-
return in_array(SoftDeletes::class, class_uses_recursive(static::class));
2318+
return static::$isSoftDeletable[static::class] ??= in_array(SoftDeletes::class, class_uses_recursive(static::class));
22982319
}
22992320

23002321
/**
23012322
* Determine if the model is prunable.
23022323
*/
23032324
protected function isPrunable(): bool
23042325
{
2305-
return in_array(Prunable::class, class_uses_recursive(static::class)) || static::isMassPrunable();
2326+
return self::$isPrunable[static::class] ??= in_array(Prunable::class, class_uses_recursive(static::class)) || static::isMassPrunable();
23062327
}
23072328

23082329
/**
23092330
* Determine if the model is mass prunable.
23102331
*/
23112332
protected function isMassPrunable(): bool
23122333
{
2313-
return in_array(MassPrunable::class, class_uses_recursive(static::class));
2334+
return self::$isMassPrunable[static::class] ??= in_array(MassPrunable::class, class_uses_recursive(static::class));
23142335
}
23152336

23162337
/**

0 commit comments

Comments
 (0)