我有模型
User
在保存时对每条记录执行 touchOwners。
<?php
class User extends Eloquent {
protected $touches = array('userDesk');
[...]
}
当我想创建一条新记录时,问题就来了: Eloquent 地尝试触摸相关表中不存在的字段(既然用户是新创建的,那么怎么会有引用的记录)到相关表中的该用户?!?...)。
如何暂时禁用此功能,以便我可以创建新用户而不会引发异常:
Call to undefined method Illuminate \ Database \ Eloquent \ Collection :: touchOwners ()
?
我尝试过这个:
$user->setTouchedRelations(array());
但是当然没用...
最佳答案
要在不触摸
的情况下保存模型,请将false
传递给save
方法:
$someModel = new SomeModel;
... // do something with your model
$someModel->save(['touch' => false]);
当然setTouchedRelations
也可以工作:
$someModel = new SomeModel;
... // do what you need
$someModel->setTouchedRelations([]);
$someModel->save();
关于php - Laravel - Eloquent - 暂时禁用 touchOwners,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26281595/