155 lines
3.9 KiB
PHP
155 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property integer $id
|
|
* @property integer $range_id
|
|
* @property string $seo_title
|
|
* @property string $seo_description
|
|
* @property string $label
|
|
* @property string $slug
|
|
* @property string $description
|
|
* @property string $picture
|
|
* @property string $certifications
|
|
* @property string $nutritionnal_values
|
|
* @property string $list
|
|
* @property boolean $status
|
|
* @property integer $order
|
|
* @property string $created_at
|
|
* @property string $updated_at
|
|
* @property Recipe[] $recipes
|
|
* @property Range $range
|
|
* @property Home[] $homes
|
|
* @property Home[] $homes
|
|
*/
|
|
class Product extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'range_id',
|
|
'seo_title',
|
|
'seo_description',
|
|
'label',
|
|
'slug',
|
|
'description',
|
|
'picture',
|
|
'certifications',
|
|
'nutritionnal_values',
|
|
'list',
|
|
'status',
|
|
'order',
|
|
'created_at',
|
|
'updated_at'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'certifications' => 'array',
|
|
'nutritionnal_values' => 'json',
|
|
'list' => 'json',
|
|
];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Undocumented function
|
|
*
|
|
* @param Model $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(Model $model)
|
|
{
|
|
self::assertFieldIsUnique('label', $model->label);
|
|
self::assertFieldIsUnique('slug', $model->slug);
|
|
ToolboxHelper::assertNoInternalBoUrls('nutritionnal_values', $model->nutritionnal_values);
|
|
ToolboxHelper::assertNoInternalBoUrls('list', $model->list);
|
|
$model->nutritionnal_values = clean($model->nutritionnal_values);
|
|
$model->list = clean($model->list);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the field is unique.
|
|
*
|
|
* @param string $nameField
|
|
* @param mixed $valueField
|
|
* @param integer|null $id
|
|
* @return void
|
|
*/
|
|
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function recipes()
|
|
{
|
|
return $this->belongsToMany('App\Models\Recipe', 'recipes_products');
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function range()
|
|
{
|
|
return $this->belongsTo('App\Models\Range');
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function homes1()
|
|
{
|
|
return $this->hasMany('App\Models\Home', 'part2_product1_id');
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function homes2()
|
|
{
|
|
return $this->hasMany('App\Models\Home', 'part2_product2_id');
|
|
}
|
|
}
|