由于是我们自己定义的事件,所以触发的时候也必须手动触发,在需要触发的地方调用模型里面的一个fireModelEvent方法即可。不过由于该方法是protected的,所以只能在自己定义的模型方法里面,当然如果通过反射来调用,或许可以直接在$user对象上触发也说不定,这个我没试,大家可以自行测试下。
class User extends Authenticatable {
use Notifiable;
protected $observables = [
'customing', 'awesoming'
];
public function custom(){
if ($this->fireModelEvent('customing') === false) {
return false;
}
//TODO
if ($this->fireModelEvent('customed') === false) {
return false;
}
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号