SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行[下拉列表的0值]

时间:2019-05-23 03:05:53

标签: laravel

我尝试从列名表1到表2输入一个值。它出现了,但第一行的值是0。它收到错误消息。

Error

控制器

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

    public function store(Request $request)
    {
        request()->validate([
            'nama' => 'required|unique:kamar_theresia,nama',
            'idkelas' => 'required',
            'ketersediaan' => 'required',
        ]);

        $input = $request->all();

        $kamar_theresia = Kamar_Theresia::create($input);

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

查看

<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>

1 个答案:

答案 0 :(得分:1)

create函数中,更改检索资源的行以包含id

因此,请添加它:

$kelas_theresia = Kelas_Theresia::pluck('name', 'id')->all();

因此这将使用正确的商品ID,并且在您存储时将是正确的ID,因为现在您的值就是名称。