【问题标题】:Undefined property: Illuminate\Database\Eloquent\Builder::$password in Laravel未定义的属性:Laravel 中的 Illuminate\Database\Eloquent\Builder::$password
【发布时间】:2018-09-29 11:38:10
【问题描述】:

我正在使用 Laravel Hashing 的散列算法。

但出现此错误

未定义属性:Illuminate\Database\Eloquent\Builder::$password

这是我的功能

public function signin(LoginFormValidation $request)
{
    $user_password = $request->password;

    $data = User::where('email','=',$request->email); 

    if (Hash::check($user_password, $data->password, flase))
    {
        echo "success";
    }
    else
    {
        echo "still not";
    }
}

【问题讨论】:

  • 不是flasefalse,你应该使用first()或者get()

标签: php laravel


【解决方案1】:

改变这一行:

$data = User::where('email','=',$request->email);

$data = User::where('email','=',$request->email)->first();

然后改变:

if (Hash::check($user_password, $data->password, flase))

if (Hash::check($user_password, optional($data)->password))

【讨论】:

  • @Sohel0415......它给出了新的错误`尝试获取非对象的属性'密码'
  • 你用的是什么版本的 laravel?
  • 表示数据库中没有给定邮箱的用户
  • @Sohel0415...新错误显示local.ERROR: Type error: Argument 3 passed to Illuminate\Hashing\BcryptHasher::check() must be of the type array, boolean given...
  • @Sohel0415...它仍然给出Undefined property: Illuminate\Support\Facades\Request::$password ....怎么样!!!
【解决方案2】:

试试这个

public function signin(Request $request)  //chnaged object to Request
{
    $user_password = $request->password;

    $data = User::where('email',$request->email)->first();  // Added first()

    if ($data  && Hash::check($user_password, $data->password, false)) // typo error false
    {
        echo "success";
    }
    else
    {
        echo "still not";
    }
}

【讨论】:

    【解决方案3】:

    让你的代码更好,我在你的 IF 语句中添加了一个修复和一个额外的检查。对于$data 未获得有效结果的情况,需要进行此额外检查。

    public function signin(LoginFormValidation $request)
    {
        $user_password = $request->password;
    
        $data = User::where('email','=',$request->email)->first(); 
    
        if ($data && Hash::check($user_password, $data->password))
        {
            echo "success";
        }
        else
        {
            echo "still not";
        }
    }
    

    【讨论】:

    • .....错误已更改并给出新错误Type error: Argument 3 passed to Illuminate\Hashing\BcryptHasher::check() must be of the type array, boolean given
    • 你可以跳过第三个参数,你为什么需要它?我会根据你的要求修改我的答案
    【解决方案4】:

    我可以看看你的模特User 吗? 我认为您应该从 protected $hidden 属性中删除 password 然后再试一次。 如果你成功了请告诉我

    【讨论】:

      猜你喜欢
      • 2016-09-05
      • 1970-01-01
      • 2016-11-27
      • 2016-07-08
      • 2014-11-23
      • 2017-03-05
      • 2020-05-04
      • 2016-06-02
      • 2020-11-23
      相关资源
      最近更新 更多