137 lines
3.1 KiB
PHP
137 lines
3.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use App\Casts\HtmlColor;
|
||
|
|
use App\Lib\ToolboxHelper;
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Validation\Rules\Unique;
|
||
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
|
||
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudType;
|
||
|
|
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
|
||
|
|
use LVR\Colour\Hex;
|
||
|
|
|
||
|
|
class Label extends Model
|
||
|
|
{
|
||
|
|
use HasFactory;
|
||
|
|
use IsCrudModel;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Crud Label
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $crudLabelColumn = 'name';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Crud Value
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $crudValueColumn = 'id';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Indicates if the model should be timestamped.
|
||
|
|
*
|
||
|
|
* @var boolean
|
||
|
|
*/
|
||
|
|
public $timestamps = false;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The attributes that should be cast.
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $casts = [
|
||
|
|
'color' => HtmlColor::class,
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The attributes that are mass assignable.
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $fillable = [
|
||
|
|
'name', 'color'
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Site
|
||
|
|
*
|
||
|
|
* @param array $attributes
|
||
|
|
*/
|
||
|
|
public function __construct(array $attributes = [])
|
||
|
|
{
|
||
|
|
self::$editableProperties = [
|
||
|
|
'name' => [
|
||
|
|
'name' => trans('Libellé'),
|
||
|
|
'type' => CrudType::string(),
|
||
|
|
'validate' => [
|
||
|
|
'required',
|
||
|
|
'min:2',
|
||
|
|
'max:255',
|
||
|
|
(new Unique('labels', 'name'))->ignore(request()->label)
|
||
|
|
],
|
||
|
|
'required' => true,
|
||
|
|
'nullable' => false,
|
||
|
|
'actions' => [
|
||
|
|
CrudAction::viewAny(),
|
||
|
|
CrudAction::create(),
|
||
|
|
CrudAction::update(),
|
||
|
|
]
|
||
|
|
],
|
||
|
|
'color' => [
|
||
|
|
'label' => trans('Couleur'),
|
||
|
|
'type' => CrudType::color(),
|
||
|
|
'validate' => ['required', new Hex()],
|
||
|
|
'required' => true,
|
||
|
|
'nullable' => false,
|
||
|
|
'actions' => [
|
||
|
|
CrudAction::viewAny(),
|
||
|
|
CrudAction::create(),
|
||
|
|
CrudAction::update(),
|
||
|
|
]
|
||
|
|
]
|
||
|
|
];
|
||
|
|
parent::__construct($attributes);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Perform any actions required after the model boots.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
protected static function booted()
|
||
|
|
{
|
||
|
|
static::creating(function (self $model) {
|
||
|
|
ToolboxHelper::assertIsUnique($model, ['name']);
|
||
|
|
});
|
||
|
|
static::updating(function (self $model) {
|
||
|
|
ToolboxHelper::assertIsUnique($model, ['name']);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// * RELATIONSHIPS
|
||
|
|
|
||
|
|
/**
|
||
|
|
* BelongsToMany Ticket relation
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||
|
|
*/
|
||
|
|
public function tickets()
|
||
|
|
{
|
||
|
|
return $this->belongsToMany(Ticket::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* BelongsTo Site relation
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
|
|
*/
|
||
|
|
public function site()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Site::class);
|
||
|
|
}
|
||
|
|
}
|