dongmu4591 2016-11-28 09:10
浏览 208
已采纳

当主键名列不是id时,在laravel中进行验证的唯一规则

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 where nisn = 1211 and id <> 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.

  • 写回答

2条回答 默认 最新

  • dtc66318 2016-11-28 10:57
    关注

    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',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题