Skip to content

[12.x] Cache isSoftDeletable(), isPrunable(), and isMassPrunable() directly in model #56078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,27 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
*/
protected static string $collectionClass = Collection::class;

/**
* Cache of soft deletable models.
*
* @var array<class-string<self>, bool>
*/
protected static array $isSoftDeletable;

/**
* Cache of prunable models.
*
* @var array<class-string<self>, bool>
*/
protected static array $isPrunable;

/**
* Cache of mass prunable models.
*
* @var array<class-string<self>, bool>
*/
protected static array $isMassPrunable;

/**
* The name of the "created at" column.
*
Expand Down Expand Up @@ -2294,23 +2315,23 @@ public function setPerPage($perPage)
*/
public static function isSoftDeletable(): bool
{
return in_array(SoftDeletes::class, class_uses_recursive(static::class));
return static::$isSoftDeletable[static::class] ??= in_array(SoftDeletes::class, class_uses_recursive(static::class));
}

/**
* Determine if the model is prunable.
*/
protected function isPrunable(): bool
{
return in_array(Prunable::class, class_uses_recursive(static::class)) || static::isMassPrunable();
return self::$isPrunable[static::class] ??= in_array(Prunable::class, class_uses_recursive(static::class)) || static::isMassPrunable();
}

/**
* Determine if the model is mass prunable.
*/
protected function isMassPrunable(): bool
{
return in_array(MassPrunable::class, class_uses_recursive(static::class));
return self::$isMassPrunable[static::class] ??= in_array(MassPrunable::class, class_uses_recursive(static::class));
}

/**
Expand Down