I have try laravel 5.2 framework for my homework, but i have an error like this when i am trying to update data
QueryException in Connection.php line 729: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from
siswa
wherenisn
= 1211 andid
<> 14)
In my form update i have apply validation method like this
@if (isset($siswa))
{!! Form::hidden('id_siswa',$siswa->id_siswa) !!}
@endif
@if ($errors->any())
<div class="form-group {{ $errors->has('nisn') ? 'has-error':'has-success' }} ">
@else
<div class="form-group">
@endif
{!!Form::label('nisn','NISN:', ['class'=>'control-label'])!!}
{!!Form::text( 'nisn',null,['class'=>'form-control'])!!}
@if ($errors->has('nisn'))
<span class="help-block"> {{ $errors->first('nisn') }} </span>
@endif
</div>
...
...
<div class="form-group">
{!!Form::submit( $submitButtonText,['class'=>'btn btn-primary form-control'])!!}
</div>
the 'nisn' field must unique, so in my controller i have update method like this
public function update($id_siswa, Request $request)
{
$siswa = Siswa::findOrFail($id_siswa);
$input = $request->all();
$validator = Validator::make($input, [
'nisn' => 'required|string|size:4|unique:siswa,nisn,'.$request->input('id_siswa'),
'nama_siswa' => 'required|string|max:30',
'tanggal_lahir'=> 'required|date',
'jenis_kelamin' => 'required|in:L,P',
]);
if($validator->fails()) {
return redirect('siswa/'.$id_siswa.'/edit')
->withInput()
->withErrors($validator);
}
$siswa->update($request->all());
return redirect('siswa');
}
In my table siswa my primary key is id_siswa, not id, i have add in my model to inform that my primary is not id, like this
protected $primaryKey = 'id_siswa';
I have try to change the validation rule like this in my controller
'nisn' => 'required|string|size:4|unique:siswa,nisn,NULL,'.$request->input('id_siswa'),
The error is gone , but when i update the other field in my form (not nisn field) the validation give me error that my nisn not unique, i want when i update the other field in my form (not nisn field) validation will ignore the rule unique in nisn, and when i update the nisn the validation will check if that nisn is used or not. Sory for my english, i hope someone can give me solution.