取消事件触发
有时候你的操作可能会触发别人的事件(也不知道是哪个大佬写的,咱也不敢乱改)
分享一个取消事件的例子。
//获取model里面的事件
$dispatcher = YourModel::getEventDispatcher();
//禁用model里面的事件
YourModel::unsetEventDispatcher();
// 调用model的save方法
$yourInstance->save();
//启用事件
YourModel::setEventDispatcher($dispatcher);
//Visitor model有saved事件,某些地方某些情况监听到后做一些其他操作
protected $dispatchesEvents = [
'saved' => VisitorSavedEvent::class,
];
//有时候不需要触发事件,是可以取消的,可以调用model的update方法避开save()或者取消事件(推荐后者)。
// 获取来访信息
$visitor = Visitor::where('id', $request->visitor_id)->first();
//取消事件触发
$dispatcher = Visitor::getEventDispatcher();
Visitor::unsetEventDispatcher();
$ret = $visitor->save();
Visitor::setEventDispatcher($dispatcher);
参考链接:
https://stackoverflow.com/questions/294078...
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 5年前 自动加精
推荐文章: