【发布时间】:2022-02-15 00:39:01
【问题描述】:
HasMany 关系类上有 save 和 saveMany 方法,但 dissociate(Many)/detach(Many) 方法在哪里?也没有内置的方法来获取反向关系方法,那么将 id/模型数组与 HasMany 关系对象分离的最佳方法是什么。
目前我正在使用:
$hasMany = $parent->theRelationship(); // Get the relationship object.
$child = $hasMany->getRelated(); // Get an empty related model.
$key = $hasMany->getForeignKeyName(); // Get the name of the column on the child to set to NULL.
$child->findMany($IDs)->each(function($model) use ($key) {
$model->$key = NULL;
$model->save();
});
这可能会更短,例如:
$hasMany = $parent->theRelationship();
$hasMany->dissociate($IDs);
如果您从 Taylor 那里得到任何关于他为什么没有实现此功能的官方回答,我会在 GitHub 上看到他关闭此类功能请求。
【问题讨论】: