用外键Laravel更新表单

时间:2019-05-26 13:14:47

标签: laravel

我有一个包含3个数据的表格。一个数据是来自另一个表的已使用外键链接的数据。我应该添加什么,以便已保存的表中的数据可以显示在编辑表单中并可以更新它?

查看

<div class="col-xs-12 col-sm-12 col-md-12">
  <div class="form-group">
    <strong>Kelas:</strong>
    {!! Form::select('idkelas', $kelas_theresia, [], array('class' => 'form-control')) !!}
  </div>
</div>

控制器

public function edit($id)
    {
        $kamar_theresia = Kamar_Theresia::find($id);
        $kelas_theresia = Kelas_Theresia::pluck('name','id')->all();
        return view('kamar_theresia.edit',compact('kamar_theresia','kelas_theresia'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */

    public function update(Request $request, $id)
    {
         request()->validate([
            'nama' => 'required',
            'idkelas' => 'required',
            'ketersediaan' => 'required',
        ]);

        $input = $request->all();
        $kamar_theresia = Kamar_Theresia::find($id);
        $kamar_theresia->update($input);

        return redirect()->route('kamar_theresia.index')
                        ->with('success','Kamar Theresia updated successfully');
    }

1 个答案:

答案 0 :(得分:0)

在“递归保存模型和关系”部分中查看文档:

  https://laravel.com/docs/5.8/eloquent-relationships#inserting-and-updating-related-models

这是一个示例:

$client = Client::find($id);
$client->name = "Daniel";
$client->addressInfo->street = 'Daniel's street';
$client->push();

致谢

相关问题