TIP 02 / 12

Create Artisan Commands for Repeatable Maintenance.

Turn recurring operational work into a command so it can be run manually, scheduled, logged, and reviewed like the rest of the application:

bash
php artisan make:command PruneExpiredInvitations

Give the command a clear signature and description, then keep the task in its handle() method:

code
protected $signature = 'invitations:prune {--dry-run}';
protected $description = 'Remove expired, unaccepted invitations';

public function handle(): int
{
    $query = Invitation::whereNull('accepted_at')
        ->where('expires_at', '<', now());

    $count = (clone $query)->count();

    if ($this->option('dry-run')) {
        $this->info("{$count} expired invitations found.");
        return self::SUCCESS;
    }

    $query->delete();
    $this->info("Removed {$count} expired invitations.");

    return self::SUCCESS;
}

The dry-run option lets you inspect the expected effect before deleting records. Schedule the command with Laravel's task scheduler when it should run automatically.