62 lines
1.4 KiB
PHP
62 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class IssueAttachment extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'issue_id',
|
|
'user_id',
|
|
'file_name',
|
|
'file_path',
|
|
'file_size',
|
|
'mime_type',
|
|
];
|
|
|
|
public function issue(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Issue::class);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function getFileSizeHumanAttribute(): string
|
|
{
|
|
$bytes = $this->file_size;
|
|
$units = ['B', 'KB', 'MB', 'GB'];
|
|
|
|
for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
|
|
$bytes /= 1024;
|
|
}
|
|
|
|
return round($bytes, 2) . ' ' . $units[$i];
|
|
}
|
|
|
|
public function getDownloadUrlAttribute(): string
|
|
{
|
|
return route('admin.issues.attachments.download', $this);
|
|
}
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::deleting(function ($attachment) {
|
|
// Delete file from storage when attachment record is deleted
|
|
if (Storage::exists($attachment->file_path)) {
|
|
Storage::delete($attachment->file_path);
|
|
}
|
|
});
|
|
}
|
|
}
|