67 lines
1.5 KiB
PHP
67 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Database\Factories\PressReleaseAttachmentFactory;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class PressReleaseAttachment extends Model
|
|
{
|
|
/** @use HasFactory<PressReleaseAttachmentFactory> */
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'press_release_id',
|
|
'disk',
|
|
'path',
|
|
'original_name',
|
|
'mime',
|
|
'size',
|
|
'title',
|
|
'description',
|
|
'sort_order',
|
|
'legacy_portal',
|
|
'legacy_id',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'size' => 'integer',
|
|
'sort_order' => 'integer',
|
|
'deleted_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function pressRelease(): BelongsTo
|
|
{
|
|
return $this->belongsTo(PressRelease::class);
|
|
}
|
|
|
|
public function url(): ?string
|
|
{
|
|
if (blank($this->path)) {
|
|
return null;
|
|
}
|
|
|
|
if ($this->disk === 'public') {
|
|
return asset('storage/'.ltrim((string) $this->path, '/'));
|
|
}
|
|
|
|
try {
|
|
$disk = Storage::disk($this->disk);
|
|
|
|
if (method_exists($disk, 'url')) {
|
|
return $disk->url($this->path);
|
|
}
|
|
} catch (\Throwable) {
|
|
return null;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|