'decimal:2', 'stock_minimum' => 'decimal:2', 'prix_unitaire' => 'decimal:2', 'conditionnement_quantite' => 'decimal:2', 'date_expiration' => 'date', ]; /** * Get the fournisseur that owns the product. */ public function fournisseur(): BelongsTo { return $this->belongsTo(Fournisseur::class); } /** * Get the category that owns the product. */ public function category(): BelongsTo { return $this->belongsTo(ProductCategory::class); } /** * Handle image upload */ public function uploadImage($image) { if ($image) { // Delete old image if exists if ($this->image) { $this->deleteImage(); } // Store the new image $imageName = time() . '_' . uniqid() . '.' . $image->getClientOriginalExtension(); $imagePath = $image->storeAs('products', $imageName, 'public'); $this->image = $imagePath; $this->save(); return $imagePath; } return null; } /** * Delete the product image */ public function deleteImage() { if ($this->image) { Storage::disk('public')->delete($this->image); $this->image = null; $this->save(); } } /** * Get the full URL of the image */ public function getImageUrlAttribute() { if ($this->image) { return Storage::url($this->image); } return null; } /** * Boot the model */ protected static function boot() { parent::boot(); static::deleting(function ($product) { // Delete the image when the product is deleted $product->deleteImage(); }); } }