我想使用它的id从缓存中删除一个特定的模型。这在控制器中按预期工作,但不使用模型闭包。
我在AppModelsPost
:中拥有的内容
use IlluminateSupportFacadesCache;
protected static function booted()
{
static::updated(function ($post) {
Cache::forget('post:'.$post->id);
});
}
如果我在控制器中执行Cache::forget('post:'.$post->id);
,它就会工作。
我错过了什么?
请确保您实际上正在更改模型上的值,因为updated
事件只有在模型脏的时候才会触发,正如您在这里看到的那样。
然而,每当您调用save()
方法时,saved
事件就会激发,如您在这里看到的:
protected static function booted()
{
static::saved(function ($post) {
Cache::forget('post:'.$post->id);
});
}
来自文档:
retrieved
事件将在从数据库。首次保存新模型时CCD_ 7和CCD_。如果模型已存在于数据库和保存方法被调用,即updating
/updated
事件将启动。然而,在这两种情况下,saving
/saved
事件将开火。