Laravel-如何从多个表关系laravel创建和更新字段?

时间:2018-07-14 19:22:29

标签: php laravel laravel-5.5

我有两个模型之间的关系,当用户创建评论时,我需要将其存储在一个表中并更新其他表中的状态字段,这应该以相同的形式提交评论。我收到错误未定义变量:id

我的控制器

 public function reply($id)
    {
    if (Auth::check())
        {
        $issue = Issue::where('user_id', Auth::id())->where('id', $id)->firstOrFail();
        $company = $issue->company;
        $interaction = $issue->interaction;
        return view('issues.reply', compact('issue', 'company', 'interaction'));
        }
      else
        {
        return redirect()->guest('/logon');
        }
    }

public function postComment(Request $request)
    {
    $this->validate($request, ['description' => '']);
    $interaction = IssueInteraction::create(['issue_report_id' => $request->input('issue_report_id') , 'user_id' => Auth::user()->id, 'description' => $request->input('description') , 'owner' => 'USER', ]);
    if ($interaction->save())
        {
        $issue = Issue::where('id', $id)->findOrFail->id;
        $issue->updateOrCreate->status = $request->status;
        $issue->save();
        }

    $interaction->created_at = date('Y-m-d H:i:s.u');
    return redirect()->back()->with("status", "Your reply has be submitted.");
    }

我的路线

Route::get('/account/issues/{id}', 'IssueInteractionController@reply');
Route::post('interaction', 'IssueInteractionController@postComment');

我的表单

 <form class="form-secondary" action="{{ url('/interaction') }}" method="POST">
   {!! csrf_field() !!}
   <input type="hidden" name="issue_report_id" value="{{ $issue->id }}">
   <textarea rows="10" id="description" class="form-control" name="description"></textarea>
   <input type="hidden" id="status" name="status" value="2">
   <button type="submit" class="btn btn-lg btn-block btn-primary shadow mt-4">@lang('app.Send Reply')</button>
</form>

预先感谢

1 个答案:

答案 0 :(得分:0)

我认为这就是您要在postComment()方法中更改的内容。也就是说,如果issue_report_id是问题模型ID的对应密钥。

    $issue = Issue::where('id', $interaction->issue_report_id)->first()->id;