| 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); | |
| } | |
| } | |