ldc-wenn-dann-mit-respekt/app/Models/Range.php
Alexis Gousseau a333993456 add seeders
2023-03-16 09:47:16 +01:00

114 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Ranges\PictosEnum;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $id
* @property string $seo_title
* @property string $seo_description
* @property string $label
* @property string $slug
* @property boolean $picto
* @property integer $order
* @property boolean $status
* @property string $created_at
* @property string $updated_at
* @property Recipe[] $recipes
* @property Product[] $products
*/
class Range extends Model
{
use HasFactory;
/**
* @var array
*/
protected $fillable = [
'seo_title',
'seo_description',
'label',
'slug',
'picto',
'order',
'status',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'picto' => PictosEnum::class,
];
// * 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);
});
}
// * METHODS
/**
* Undocumented function
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
self::assertFieldIsUnique('label', $model->label);
self::assertFieldIsUnique('slug', $model->slug);
}
/**
* 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\HasMany
*/
public function recipes()
{
return $this->hasMany('App\Models\Recipe');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products()
{
return $this->hasMany('App\Models\Product');
}
}