Eloquent: 附加相关记录
Eloquent: attach relative records
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);
}
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);
}