option('delete') && $this->option('to-sql')) { $this->fail("Can't use both --delete and --to-sql at the same time"); } if ($this->option('delete')) { if (!$this->option('force')) { if (!$this->confirm('Do you really want to delete all action_logs with nonexistent items or targets?')) { $this->fail("Command canceled"); } } } $map = [ Accessory::class => 'accessories', Asset::class => 'assets', AssetModel::class => 'models', Component::class => 'components', Consumable::class => 'consumables', License::class => 'licenses', Location::class => 'locations', User::class => 'users', ]; $rows = []; foreach (['item', 'target'] as $prefix) { foreach ($map as $class => $table) { $query = Actionlog::withTrashed()->where($prefix . '_type', $class)->whereNotIn($prefix . '_id', $class::select('id')->withTrashed()); if ($this->option('delete')) { $count = $query->delete(); } elseif ($this->option('to-sql')) { $count = $query->toRawSql(); } else { $count = $query->count(); } $rows[] = [$prefix, $class, $table, $count]; // DELETE FROM action_logs where {$prefix}_type='$class' /* make sure about backslashes! */ AND {$prefix}_id NOT IN (SELECT id FROM $table) } } if (!$this->option('delete')) { $this->line("Hint: use --delete to delete these entries instead of counting them"); $this->newLine(); } $this->table(['Attribute', 'Class', 'Table', 'Count'], $rows); } }