laravel 显示未定义索引的错误:密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41117495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:54:43  来源:igfitidea点击:

Showing an error of Undefined index: password

phplaravel

提问by Rafik malek

I my session controller i code for the verify email ,login and changepassword but getting an error

我的会话控制器我为验证电子邮件、登录和更改密码编码,但收到错误

ErrorException in EloquentUserProvider.php line 116: Undefined index: password.

EloquentUserProvider.php 第 116 行中的 ErrorException:未定义索引:密码。

and getting the error from line code

并从行代码中获取错误

if (!Auth::attempt($credentials_verifiy))

<?php
namespace App\Http\Controllers;

use Request;
use Response;
//----models--------
use App\Site;
use App\Jobs;


use Auth;
use DB;
use Validator;
use Redirect;

//use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
class SessionController extends Controller {

    public function index(){
        return Redirect::to('login')->with('alert-success', 'test awrnning message.');
    }
    public function store()
    {
             $input = Request::only('username', 'email', 'password');
             $credentials = [
                 'username' => Request::get('username'),
                 'password' => Request::get('password')
             ];

            if (!Auth::attempt($credentials))
            {
                return Redirect::back()->with('alert-danger', 'Username or password do not match.');
            }
            else
            {
                $credentials_verifiy = [
                    'verified_email' => '1'
                ];
                if (!Auth::attempt($credentials_verifiy))
                {
                    return Redirect::back()->with('alert-danger', 'Please verify your email.');
                }
                else
                {
                    if(Auth::user()->last_login_at=='' || Auth::user()->last_login_at==null)
                    {
                        return redirect('/change_password');
                    }
                    else
                    {
                        return redirect('/tests');
                    }
                }
            }
    }
}

采纳答案by Bara' ayyash

In order to skip this Exception you can use this line of code

为了跳过这个异常,你可以使用这行代码

array_get('password', $input);

if the array $inputhas no index passwordit will not throw an Exception, it will return null, you can make it returns what you want : array_get('password', $input, ' ');

如果数组$input没有索引,password它不会抛出异常,它会返回 null,你可以让它返回你想要的:array_get('password', $input, ' ');

回答by Alexey Mezenin

$credentialsarray in Auth::attempt($credentials)should contain value with passwordkey, that's why you're getting the error. It's hardcoded in validateCredentials()method which Auth::attempt()is using:

$credentials数组中Auth::attempt($credentials)应包含带password键的值,这就是您收到错误的原因。它在 validateCredentials()方法中硬编码,Auth::attempt()方法使用:

public function validateCredentials(UserContract $user, array $credentials)
{
    $plain = $credentials['password'];

    return $this->hasher->check($plain, $user->getAuthPassword());
}