File size: 1,218 Bytes
70ba896 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Product extends Model
{
protected $fillable = ['name', 'price', 'description', 'image', 'Amount', 'game', 'slug'];
protected static function boot()
{
parent::boot();
static::creating(function ($product) {
if (empty($product->slug)) {
$product->slug = Str::slug($product->name);
}
});
static::updating(function ($product) {
if ($product->isDirty('name') && empty($product->slug)) {
$product->slug = Str::slug($product->name);
}
});
}
public function getImageAttribute($value)
{
if (!$value) {
return null;
}
// If it's already a full URL, return as is
if (str_starts_with($value, 'http')) {
return $value;
}
// If it starts with 'storage/', it's already formatted
if (str_starts_with($value, 'storage/')) {
return asset($value);
}
// Otherwise, prepend 'storage/'
return asset('storage/' . $value);
}
}
|