32 lines
892 B
PHP
32 lines
892 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
use App\Models\ActivityLog;
|
||
|
|
use Carbon\Carbon;
|
||
|
|
|
||
|
|
class CleanActivityLogs extends Command
|
||
|
|
{
|
||
|
|
// Nama perintah yang akan dipanggil di terminal
|
||
|
|
protected $signature = 'activity-log:clean';
|
||
|
|
|
||
|
|
// Deskripsi perintah
|
||
|
|
protected $description = 'Menghapus log aktivitas yang sudah lebih dari 30 hari';
|
||
|
|
|
||
|
|
public function handle()
|
||
|
|
{
|
||
|
|
$days = 30;
|
||
|
|
$date = Carbon::now()->subDays($days);
|
||
|
|
|
||
|
|
// Menghitung jumlah log yang akan dihapus untuk laporan di terminal
|
||
|
|
$count = ActivityLog::where('created_at', '<', $date)->count();
|
||
|
|
|
||
|
|
if ($count > 0) {
|
||
|
|
ActivityLog::where('created_at', '<', $date)->delete();
|
||
|
|
$this->info("Berhasil menghapus {$count} log lama.");
|
||
|
|
} else {
|
||
|
|
$this->info("Tidak ada log lama yang perlu dihapus.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|