У меня есть класс, который наследует базовый класс и использует черту ... Я приведу код ниже ..
Базовый класс использует в основном для проверки перед спасением, используя для этогособытие сохранения в загрузке.
Эта особенность состоит в том, чтобы указывать классу использовать uuid в атрибуте id. Эта особенность использует событие создания загрузки.
В самом классе,событие сохранения загрузки используется, чтобы проверить, существует ли активная запись.
В этом коде событие создания черты не запускается ... Я не могу сделать сохранение, потому что uuid не генерируется ... если явозьмите метод загрузки в последнем классе, событие создания выполнено ...
что-то, чего я не вижу ... кто-нибудь знает, что может происходить?
MAIN CLASS
class AcademicYear extends BaseModel
{
use UseUuid;
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::saving(function($model)
{
if($model->attributes['disable'] == false){
$model->searchActiveRecord();
}
});
}
public function searchActiveRecord(){
if ($this::where('disable', false)->count() >= 1){
throw new \App\Exceptions\OperationNotAllowed('operation not allowed', 'there is an active record', '422');
}
return true;
}
}
БАЗОВАЯ МОДЕЛЬ
class BaseModel extends Model
{
/**
* If the model will be validated in saving
*
* @var bool
*/
protected static $validate = true;
/**
* Rules that will be used to validate the model
*
* @var array
*/
protected $validationRules = [];
/**
* Create a new base model instance.
*
* @param array $attributes
* @return void
*/
public function __construct($attributes = [])
{
parent::__construct($attributes);
}
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::saving(function($model)
{
if ($model::$validate) {
$model->validate();
}
});
}
/**
* Execute validation of model attributes.
*
* @return void
*/
public function validate()
{
$validator = Validator::make($this->attributesToArray(), $this->validationRules);
if($validator->fails()) {
throw new \App\Exceptions\OperationNotAllowed('validation failed', $validator->messages(), '422');
}
return true;
}
}
TRAIT
trait UseUuid
{
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model)
{
$model->incrementing = false;
$model->keyType = 'string';
$model->{$model->getKeyName()} = Str::uuid()->toString();
});
static::retrieved(function ($model)
{
$model->incrementing = false;
});
}
}