Skip to content

Prune old database notifications #40124

Closed Answered by YilanBoy
smknstd asked this question in Ideas
Discussion options

You must be logged in to vote

I create a Notification model and use Pruning Models to clean the stale notification records.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\MassPrunable;

class Notification extends Model
{
    use HasFactory, MassPrunable;

    public function prunable(): Builder
    {
        return static::whereNotNull('read_at')
            ->where('read_at', '<=', now()->subWeek());
    }
}

then add this prune task in Console/Kernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command('model:prune')->daily();
}

It's worked fo…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by smknstd
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants