Reputation: 491
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.
Upvotes: 3
Views: 7548
Reputation: 1
So you are right about the except parameter, it is the id that you want to be excluded from the check.
And idColumn is optional in case your column is not called id but user_id for example. So you will use:
'required|unique:users,email,'.$this->id . ',user_id';
Upvotes: 0
Reputation: 357
If you want to be consistent with the model, you also don't need to hardcode primary key, as @JiFus suggested, instead use getKeyName
from the model:
'nisn' => 'required|string|size:4|unique:siswa,nisn,'.$id_siswa.',' . $this->getKeyName()
Now whenever you may want to change your model's primary key, you don't need to fix this in many places.
Upvotes: 0
Reputation: 968
In Laravel 5.2 there is a neater way to validate your data. I would recommend to use the validator like this:
$this->validate($request, [
'field' => 'rules',
]);
If you use the validator like I mentioned above, you don't need the part in your code shown below anymore, because Laravel will redirect back automatically for you.
if($validator->fails()) {
return redirect('siswa/'.$id_siswa.'/edit')
->withInput()
->withErrors($validator);
}
Besides this, the problem that occurs is because you are using $request->input('id_siswa')
. I guess you checked out the validation rules, but didn't understand it.
I will try to explain it clearer to you. The unique rule has an option to exclude a certain row from the unique check. This can be done by adding more options to the unique rule using commas (,
).
To exclude a certain row, you need to specify the value of the id column of the table. In your case, you also need to specify which column of the table is the id column.
I think this is what you need:
'nisn' => 'required|string|size:4|unique:siswa,nisn,'.$id_siswa.',id_siswa',
Upvotes: 3