spatie / laravel-model-cleanup

Clean up unneeded records
https://freek.dev/410-a-laravel-package-to-clean-up-models
MIT License
399 stars 42 forks source link

olderThan() not working within scope() #45

Closed skalero01 closed 4 years ago

skalero01 commented 4 years ago

I have the next code on my model

public function cleanUp(CleanupConfig $config): void
    {
        $config->olderThan(now()->subMonth())
            ->scope(function($query) {
                $query->where('causer_type', 'like', '%SystemReport%')->orWhereNull('causer_type');
            });
    }

But when executing php artisan clean:models is removing all the rows that gets with the scope function without checking the created_at column.

It works if i change the code to this:

public function cleanUp(CleanupConfig $config): void
    {
        $config->scope(function($query) {
                $query->where('causer_type', 'like', '%SystemReport%')->orWhereNull('causer_type')->where('created_at', '<', now()->subMonth());
            });
    }

Is this normal behavior? I am using the last version available

skalero01 commented 4 years ago

Sorry, found the problem, i fixed it with this:

public function cleanUp(CleanupConfig $config): void
    {
        $config->olderThan(now()->subMonth())
            ->scope(function($query) {
                $query->where(function($query) {
                    $query->where('causer_type', 'like', '%SystemReport%')->orWhereNull('causer_type');
                });
            });
    }

Regards