更新唯一属性的验证

时间:2016-10-27 06:49:28

标签: laravel controller unique

我在控制器中更新了此功能:

public function update(Request $request, $id){
    $siswa = Siswa::findOrFail($id);
    $input = $request->all();

    $validator = Validator::make($input, [
        'nisn'=>'required|string|size:4|unique:siswa,nisn'.$request->input('nisn'),
        'nama_siswa'=>'required|string|max:30', 
        'tgl_lahir'=>'required|date',
        'jns_klmin'=>'required|in:L,P',
    ]);

    if ($validator->fails()) {
        return redirect('siswa/'.$id.'/edit')->withInput()->withErrors($validator);
    }

    $siswa->update($request->all());
    return redirect('siswa');
}

其中nisn是唯一属性。然而,当我运行它时,我总是在这个屏幕上偶然发现:

Connection.php第729行中的QueryException: SQLSTATE [42S22]:未找到列:1054未知列' nisn1007'在' where子句' (SQL:选择count(*)作为siswa的聚合,其中nisn1007 = 1007)

任何帮助表示赞赏。提前致谢

1 个答案:

答案 0 :(得分:2)

您忘记了,

'nisn'=>'required|string|size:4|unique:siswa,nisn'.$request->input('nisn'),

应该是这样的:

'nisn'=>'required|string|size:4|unique:siswa,nisn,'.$request->input('nisn'),

之后:siswa,nisn