php - Laravel 雄辩的更新方法不保存父关系
问题描述
在这里,评论保存成功。但是,$lead 没有更新!我已经导致评论 Laravel 中的一对多关系。
public function update($id, UpdateLeadRequest $request)
{
$lead = Lead::find($id);
$lead->fill($request->validated());
$changeToRecall = $request->reason == 'RECALL';
if ($changeToRecall && empty($request->agent_id)) {
$lead->agent_id = Auth::id();
}
$expert = User::findOrFail($lead->expert_id);
$oldLeadStatus = $lead->status;
$oldClosedUntil = $lead->closed_until;
$leadStateChangeUseCase = new LeadStateChangeUseCase();
$leadStateChangeUseCase->setStatus($lead, $request->reason);
$leadStateChangeUseCase->setClosedUntilDate($lead, $request);
$comment = new Comment();
$comment->user_id = Auth::id();
$comment->fill($request->comment);
try {
$date = \Carbon\Carbon::parse($request->date);
} catch (Exception $e) {
$date = now('Europe/Berlin');
}
$comment->date = $date;
DB::transaction(function () use ($lead, $comment) {
$lead->comments()->save($comment); // Trying to save here
});
return Response::json(['message' => 'lead saved']);
}
public function comments(): MorphMany { return $this->morphMany(Comment::class, 'commentable'); }
解决方案
推荐阅读
- c# - 如何适当地反序列化动态 JSON?
- javascript - 将表名传递给 Javascript 文档获取元素 ID 函数
- python - 我的代码不接受字符串,它正在检查错误的变量
- python - TCP SocketServer 只接受没有 AF_INET 和 SOCK_DGRAM 传入客户端的连接
- r - 通过在 id 列后插入空格来写出数据集
- node.js - 如何在 Cloud Functions 中使用 Firebase 远程配置?
- html - 某些(移动)设备上出现奇怪的白条
- android - fontFamily 属性无法通过 textAppearance 工作
- security - 如何在 Intranet 中处理需要安全上下文的 Web 应用程序?
- arrays - 将 foreach 循环输出存储到数组