首页 > 解决方案 > 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'); }

标签: phplaraveleloquent

解决方案


推荐阅读