laravel - Eloquent:附加相关记录
问题描述
2 个模型:FMType 和 Profile。FMPType 有许多 Profile 记录(一对多关系)。
class FMPType extends Model
{
public function profiles()
{
return $this->hasMany(Profile::class, 'fmptype_id');
}
}
和
class Profile extends Model
{
public function fmptype() {
return $this->belongsTo(FMPType::class);
}
}
我需要将一些 FMPType 复制到另一个,包括相关配置文件:
public function copy(Request $request, int $fmptype)
{
$source = FMPType::findOrFail($fmptype);
// Double type
$target = $source->replicate();
$target->name = $source->name . ' (Copy)';
$target->save();
// Double profiles
foreach ($source->profiles as $profile) {
$targetProfile = $profile->replicate();
// Associate new Profile with new FMPType
// Attempt 1: add - nothing happened, works silent, link remains as in source
$target->profiles->add($targetProfile);
// Attempt 2: associate - error, no such method
$target->profiles()->associate($targetProfile);
$targetProfile->save();
}
return somewhere;
}
在这里,我无法使用 Eloquent 方法将子配置文件与父 FMPType 关联。唯一的直接分配有效:$targetProfile->fmptype_id = $target->id
,但从 Eloquent 的角度猜想这是错误的方式。
应该怎样做这样的关联?
更新 - 它是如何工作的:
public function copy(Request $request, int $fmptype)
{
$source = FMPType::findOrFail($fmptype);
// Double type
$target = $source->replicate();
$target->name = $source->name . ' (Copy)';
$target->save();
// Double profiles
foreach ($source->profiles as $profile) {
$targetProfile = $profile->replicate();
// Associate new Profile with new FMPType
// Save both relation and $targetProfile
$target->profiles()->save($targetProfile);
// This save() is not required anymore
// $targetProfile->save();
}
return somewhere;
}
解决方案
HasMany
关系没有关联,你应该使用save()
. 从我可以从您的代码复制中读取的内容是不必要的。我猜你只是想在你的$target
.
foreach ($source->profiles as $profile) {
$target->profiles()->save($targetProfile);
}
推荐阅读
- postgresql - Postgresql 使用全文 ts_query 精确匹配
- ios - 使用 inputAccessoryView 在 UICollectionViewController 上的零索引中插入项目时出现问题
- python - 试图让 has:permission 出错
- javascript - 如何隐藏/显示 C# 函数中的 html 元素?
- c# - DataGridTextColumn 中的 DataContext 绑定 ToolTipService.IsEnabled 错误
- r - 如何使用新数据自动更新闪亮的应用程序?
- gitlab - scp 在 Gitlab CI 作业期间找不到当前目录
- python - 在 Maya Python API 2.0 中连接数组属性
- angular - 父组件中的 Angular ActivatedRoute 参数订阅仅触发一次
- java - 将 Spring 升级到最新补丁版本是否安全?