ldc-doux/app/Models/Product.php
Sebastien Hattry 7a27f3f015 - fix create gamme bug
- css modifications
2022-03-04 17:59:10 +01:00

89 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Utils;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Spatie\Translatable\HasTranslations;
class Product extends Model
{
use HasFactory;
use HasTranslations;
protected $fillable = [
'gamme_id',
'name',
'slug',
'description',
'picture',
'alt_text_picture',
'pictos',
'plusproduit',
'is_published',
'is_on_top',
'is_halal',
'is_food_service',
'seo_title',
'seo_description',
];
public $translatable = [
'name',
'slug',
'description',
'picture',
'alt_text_picture',
'pictos',
'plusproduit',
'seo_title',
'seo_description',
];
protected static function boot()
{
static::creating(function (Product $product) {
if (is_a($product->picture, "SplFileInfo")) {
$product->picture = Utils::storeImage($product->picture, $product->getTable());
}
});
static::updating(function (Product $product) {
if (is_a($product->picture, "SplFileInfo")) {
$product->picture = Utils::storeImage($product->picture, $product->getTable());
}
});
static::deleted(function (Product $product) {
if ($product->picture) {
$urlPicture = public_path() . $product->picture;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
//delete des images de plus produit
$plusproduit = json_decode($product->plusproduit);
foreach ($plusproduit as $plus) {
$urlPicture = public_path() . $plus->url;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
});
parent::boot();
}
/**
* Get the Range that owns the product.
*/
public function gamme()
{
return $this->belongsTo(Gamme::class);
}
}