ldc-com-app-bo/app/Models/Document.php
2023-04-05 15:36:39 +02:00

144 lines
3.1 KiB
PHP

<?php
namespace App\Models;
use App\Enums\TypeEnum;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $id
* @property integer $uuid
* @property string $title
* @property string $type
* @property integer $directory_id
* @property integer $parent_id
* @property string $path
* @property string $lang
* @property boolean $status
* @property integer $order
*/
class Document extends Model
{
use HasFactory;
/**
* Fillable;
*
* @var string[]
*/
protected $fillable = [
'uuid',
'title',
'type',
'directory_id',
'parent_id',
'path',
'lang',
'status',
'order',
];
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
'type' => TypeEnum::class,
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::assertFieldIsUnique('title', $model->title);
self::setOrder($model);
});
}
// * METHODS
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param string $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);
}
/**
* 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;
}
/**
* Check if is directory system.
* 1 = Salon
*
* @return boolean
*/
public function getIsSystemAttribute()
{
return $this->id === 1;
}
// * RELATIONSHIPS
/**
* One To Many Relationship Method.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function directory()
{
return $this->belongsTo('App\Models\Directory', 'directory_id');
}
/**
* One To Many Relationship Method.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent()
{
return $this->belongsTo('App\Models\Documents', 'parent_id');
}
/**
* Has Many Relationship Method.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function children()
{
return $this->hasMany('App\Models\Documents', 'parent_id');
}
/**
* MorphedByMany appusers relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphedByMany
*/
public function appusers()
{
return $this->morphedByMany(AppUser::class, 'taggable');
}
}