是否可以在 Laravel 中暂时禁用事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29407818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to temporarily disable event in Laravel?
提问by codebug
I have the following code in 'saved' model event:
我在“已保存”模型事件中有以下代码:
Session::flash('info', 'Data has been saved.')`
so everytime the model is saved I can have a flash message to inform users. The problem is, sometimes I just need to update a field like 'status' or increment a 'counter' and I don't need a flash message for this. So, is it possible to temporarily disable triggering the model event? Or is there any Eloquent method like $model->save()
that doesn't trigger 'saved' event?
所以每次保存模型时,我都可以有一条闪消息通知用户。问题是,有时我只需要更新像“状态”这样的字段或增加“计数器”,而我不需要为此提供闪现消息。那么,是否可以暂时禁用触发模型事件?或者是否有任何 Eloquent 方法$model->save()
不会触发“已保存”事件?
回答by Dan
The following solution works from the Laravel version 5.7 to 6.x, for older versions check the second part of the answer.
以下解决方案适用于 Laravel 版本 5.7 到 6.x,对于旧版本,请查看答案的第二部分。
In your model add the following function:
在您的模型中添加以下函数:
public function saveWithoutEvents(array $options=[])
{
return static::withoutEvents(function() use ($options) {
return $this->save($options);
});
}
Then to save without events proceed as follow:
然后在没有事件的情况下进行保存,如下所示:
$user = User::find(1);
$user->name = 'John';
$user->saveWithoutEvents();
For more info check the Laravel 6.x documentation
有关更多信息,请查看Laravel 6.x 文档
The following solution works for Laravel 5.6 and older versions.
以下解决方案适用于Laravel 5.6 和旧版本。
In Laravel 5.6 (and previous versions) you can disable and enable againthe event observer:
在 Laravel 5.6(和以前的版本)中,您可以禁用和再次启用事件观察器:
// getting the dispatcher instance (needed to enable again the event observer later on)
$dispatcher = YourModel::getEventDispatcher();
// disabling the events
YourModel::unsetEventDispatcher();
// perform the operation you want
$yourInstance->save();
// enabling the event dispatcher
YourModel::setEventDispatcher($dispatcher);
For more info check the Laravel 5.6 documentation
有关更多信息,请查看Laravel 5.6 文档
回答by Morteza Rajabi
There is a nice solution, from Taylor's Twitter page:
有一个很好的解决方案,来自 Taylor 的 Twitter 页面:
Add this method to your base model, or if you don't have one, create a trait, or add it to your current model
将此方法添加到您的基本模型中,或者如果您没有,请创建一个特征,或将其添加到您当前的模型中
public function saveQuietly(array $options = [])
{
return static::withoutEvents(function () use ($options) {
return $this->save($options);
});
}
Then in you code, whenever you need to save your model without events get fired, just use this:
然后在您的代码中,每当您需要保存模型而不触发事件时,只需使用以下代码:
$model->foo = 'foo';
$model->bar = 'bar';
$model->saveQuietly();
Very elegant and simple :)
非常优雅和简单:)
回答by Marlon Bernal
Call the model Object then call unsetEventDispatcher after that you can do whatever you want without worrying about Event triggering
调用模型对象然后调用 unsetEventDispatcher 之后你可以做任何你想做的事而不必担心事件触发
like this one:
像这个:
$IncidentModel = new Incident;
$IncidentModel->unsetEventDispatcher();
$incident = $IncidentModel->create($data);
回答by Andy Soell
To answer the question for anyone who ends up here looking for the solution, you can disable model listeners on an instance with the unsetEventDispatcher()
method:
要为最终在这里寻找解决方案的任何人回答这个问题,您可以使用以下unsetEventDispatcher()
方法禁用实例上的模型侦听器:
$flight = App\Flight::create(['name' => 'Flight 10']);
$flight->unsetEventDispatcher();
$flight->save(); // Listeners won't be triggered
回答by Laurence
You shouldnt be mixing session flashing with model events - it is not the responsibility of the model to notify the session when something happens.
您不应该将会话闪烁与模型事件混合在一起 - 模型没有责任在发生某些事情时通知会话。
It would be better for your controller to call the session flash when it saves the model.
您的控制器在保存模型时调用会话 flash 会更好。
This way you have control over when to actually display the message - thus fixing your problem.
这样您就可以控制何时实际显示消息 - 从而解决您的问题。