<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
class Media extends Model
{
protected $fillable = ['original_name', 'path', 'mime_type', 'size', 'title'];
public function getUrlAttribute(): string
{
return asset('storage/' . $this->path);
}
public function getSizeForHumansAttribute(): string
{
$kb = $this->size / 1024;
if ($kb < 1024) return round($kb, 1) . ' KB';
return round($kb / 1024, 2) . ' MB';
}
protected static function booted()
{
static::deleting(function ($media) {
Storage::disk('public')->delete($media->path);
});
}
}