46 lines
1.1 KiB
PHP
46 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ClearDatabaseSessions extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'session:clear-db {--force : Force the operation without confirmation}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Clear all database sessions';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
if (!$this->option('force') && !$this->confirm('Are you sure you want to clear all database sessions?')) {
|
|
$this->info('Operation cancelled.');
|
|
return 0;
|
|
}
|
|
|
|
try {
|
|
$count = DB::table('sessions')->count();
|
|
DB::table('sessions')->delete();
|
|
|
|
$this->info("Successfully cleared {$count} database sessions.");
|
|
return 0;
|
|
} catch (\Exception $e) {
|
|
$this->error('Failed to clear database sessions: ' . $e->getMessage());
|
|
return 1;
|
|
}
|
|
}
|
|
}
|