I
I
Ilshat Gayanov2018-03-18 11:11:16
Laravel
Ilshat Gayanov, 2018-03-18 11:11:16

Why is there an error when validating a laravel rule?

I'm trying to verify the username and password, as a result, the error:

local.ERROR: Undefined offset: 0 {"exception":"[object] (ErrorException(code: 0)

PHP code itself:
Validator::extend('user_check', function($attribute, $value, $parameters, $validator) {
            $userDatabase = new UserEloquent;
            $user = $userDatabase->where('login', $value)->get();
            if($user[0]['login'] == $value){
                return true;
            }
                return false;
        });
        
        Validator::extend('passw_hash_check', function($attribute, $value, $parameters, $validator) {
            $userDatabase = new UserEloquent;
            $user = $userDatabase->where('hash_password', $value)->get();
            if($user[0]['hash_password'] == $value){
                return true;
            }
                return false;
        });

$validator = Validator::make($request->all(), [
                'login' => 'required|string|user_check|min:3',
                'password' => 'required|string|passw_hash_check|min:3',
            ]);

what to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anlamas, 2018-03-18
@ilshat_gayanov

// вместо этого
$userDatabase = new UserEloquent;
$user = $userDatabase->where('login', $value)->get();

// это
$user = DB::table('users')->select('id')->where('login', $value)->first();
if($user) return true;
return false;

// или
'login' => 'unique:users,login'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question