lcly-data-admin/app/Models/WormPhoto.php

34 lines
807 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
class WormPhoto extends Model
{
use HasFactory;
protected $casts = [
'uploaded_at' => 'datetime',
];
protected $fillable = [
'device_id', 'url', 'uploaded_at',
];
public function imageUrl(): Attribute
{
return Attribute::make(
get: function(mixed $value, array $attributes) {
if (preg_match('/^https?:\/\//', $attributes['url']) > 0) {
return $attributes['url'];
}
return Storage::disk('public')->url($attributes['url']);
},
);
}
}