44 lines
1.1 KiB
PHP
Executable File
44 lines
1.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute; // 必须加!
|
|
use Illuminate\Support\Facades\Storage; // 必须加!
|
|
//use Illuminate\Support\Facades\Log;
|
|
|
|
class UploadImages extends Model
|
|
{
|
|
protected $guarded = [];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'aArticleCode' => 'array',
|
|
];
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::deleting(function (UploadImages $oImage) {
|
|
if ($oImage->sFileName && Storage::disk('upload_images')->exists($oImage->sFileName)) {
|
|
Storage::disk('upload_images')->delete($oImage->sFileName);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function oImageable(): MorphTo
|
|
{
|
|
return $this->morphTo('oImageable');
|
|
}
|
|
|
|
protected function sPath(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn (string $value) => Storage::disk('upload_images')->url($value),
|
|
);
|
|
}
|
|
|
|
}
|