php - Laravel checking if name is already in use throws error -


when i'm trying check if name in use error this: sqlstate[23000]: integrity constraint violation: 1062 duplicate entry 'feknaz' key 'name'

well says it's duplicate. how make every other error in laravel ? how show "this name taken". don't know how make 1 field. code:

registersusers.php:

if ($validator->fails()) {      if (user::where('name', '=', input::get('name'))->count() > 0) {        return redirect('registruotis')->witherrors(['msg', 'the message']);     }            return redirect('registruotis')->witherrors($validator); } 

register.blade.php:

@if (count($errors)) <div id="error-box" class="alert alert-danger center-block" style="width: 350px">     <ul>         @foreach($errors->all() $error)             <li>{{ $error }}</li>         @endforeach     </ul>     </div> @endif 

i solved it

i needed put this:

 if (user::where('name', '=', input::get('name'))->count() > 0) {    return redirect('registruotis')->witherrors(['msg', 'the message']); 

before if ($validator->fails())

the validator automatically handles you:

$validate = validator:make(array(     ....     'name' => 'unique:users,name' )); 

this produce correct error when name exists in table users on column name. can customize error message if that's need do.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -