Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/public_html/dev-test/app/Console/Commands/ |
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Modules\Schedules\Models\BookingAttachment;
class DeleteExpiredAttachments extends Command
{
protected $signature = 'attachments:cleanup';
protected $description = 'Delete attachments older than 1 day from booking time';
public function handle()
{
$now = Carbon::now();
$attachments = BookingAttachment::whereHas('booking', function ($q) use ($now) {
$q->whereRaw("
DATE_ADD(
CONCAT(date, ' ', hour),
INTERVAL 1 DAY
) <= ?
", [$now]);
})->get();
$this->info('Found ' . $attachments->count() . ' expired attachments.');
foreach ($attachments as $attachment) {
// Delete file from storage
if (file_exists(public_path($attachment->file_path))) {
unlink(public_path($attachment->file_path));
}
// Delete DB record
$attachment->delete();
}
$this->info('Expired attachments deleted successfully.');
}
}