ldc-nature-and-respect/app/Models/Range.php

154 lines
3.3 KiB
PHP
Raw Normal View History

2023-03-15 13:51:01 +01:00
<?php
namespace App\Models;
2023-03-20 10:47:44 +01:00
use App\Enums\Ranges\PictoEnum;
2023-03-15 13:51:01 +01:00
use App\Lib\Helpers\ToolboxHelper;
2023-03-16 09:47:16 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-03-15 13:51:01 +01:00
use Illuminate\Database\Eloquent\Model;
2023-03-20 10:47:44 +01:00
use Illuminate\Support\Str;
use Spatie\Translatable\HasTranslations;
2023-03-15 13:51:01 +01:00
/**
* @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
{
2023-03-16 09:47:16 +01:00
use HasFactory;
use HasTranslations;
2023-03-16 09:47:16 +01:00
2023-03-15 13:51:01 +01:00
/**
* @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 = [
2023-03-20 10:47:44 +01:00
'picto' => PictoEnum::class,
2023-03-15 13:51:01 +01:00
];
/**
* The translatable columns.
2023-04-18 16:05:34 +02:00
*
* @var array
*/
public $translatable = [
'seo_title',
'seo_description',
'label',
'slug',
];
2023-03-15 13:51:01 +01:00
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
2023-03-20 10:47:44 +01:00
self::assertFieldIsUnique('label', $model->label);
self::setOrder($model);
2023-03-15 13:51:01 +01:00
});
static::updating(function (self $model) {
self::manageFields($model);
});
}
// * METHODS
/**
2023-03-20 10:47:44 +01:00
* Set the slug.
*
* @param \Illuminate\Database\Eloquent\Model $game
*
* @return void
*/
private static function setSlug(Model $model)
{
$model->slug = Str::slug($model->label);
}
/**
* Common methods function
2023-03-15 13:51:01 +01:00
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
2023-03-20 10:47:44 +01:00
self::setSlug($model);
2023-03-15 13:51:01 +01:00
}
/**
* 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);
}
2023-03-20 10:47:44 +01:00
/**
* Set order after the last element of the list.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
private static function setOrder(Model $model): void
{
$model->order = \intval(self::query()->max('order')) + 1;
}
2023-03-15 13:51:01 +01:00
// * 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');
}
}