Only until Jan 16th: coupon RESOLUTION25 for 40% off Yearly/Lifetime membership!

Read more here

Prunable trait to automatically remove models from your database

New in Laravel 8.50: You can use the Prunable trait to automatically remove models from your database. For example, you can permanently remove soft deleted models after a few days.

class File extends Model
{
use SoftDeletes;
 
// Add Prunable trait
use Prunable;
 
public function prunable()
{
// Files matching this query will be pruned
return static::query()->where('deleted_at', '<=', now()->subDays(14));
}
 
protected function pruning()
{
// Remove the file from s3 before deleting the model
Storage::disk('s3')->delete($this->filename);
}
}
 
// Add PruneCommand to your schedule (app/Console/Kernel.php)
$schedule->command(PruneCommand::class)->daily();

Tip by @Philo01

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 69 courses (1205 lessons, total 45 h 02 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses