52 lines
1.1 KiB
PHP
52 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Attachable\Models;
|
|
|
|
use App\Domains\Attachable\Enums\AttachmentType;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
#[Fillable([
|
|
'key',
|
|
'path',
|
|
'filename',
|
|
'type',
|
|
'mime_type',
|
|
'extension',
|
|
'size',
|
|
])]
|
|
class Attachment extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'attachments';
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $attachment): void {
|
|
if (! $attachment->key) {
|
|
$attachment->key = (string) Str::uuid();
|
|
}
|
|
});
|
|
}
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'type' => AttachmentType::class,
|
|
'size' => 'integer',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<AttachableAttachment, $this>
|
|
*/
|
|
public function attachables(): HasMany
|
|
{
|
|
return $this->hasMany(AttachableAttachment::class, 'attachment_id');
|
|
}
|
|
}
|