Hash 类未找到 Laravel 5,在模型级别,寻找 App\Hash?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29158431/
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 11:10:24  来源:igfitidea点击:

Hash class not found Laravel 5, at model level, looking for App\Hash?

phplaravellaravel-5

提问by AliBaba

I'm building my first Laravel application, and I'm trying to figure out how I should be hashing passwords at a model level.

我正在构建我的第一个 Laravel 应用程序,我试图弄清楚我应该如何在模型级别散列密码。

The issue is, when trying to use the Laravel Hash::class, it can't be found. I've attempted to look up the relevant API documentation, but can't find anything apart from some references to Illuminatenamespace classes - and from what I gather Hash::should be globally available?

问题是,在尝试使用 LaravelHash::类时,找不到它。我试图查找相关的 API 文档,但除了对Illuminate命名空间类的一些引用之外找不到任何东西- 从我收集的内容来看Hash::应该是全局可用的?

I'm new to PHP namespacing, and I'm guessing this may have something to do the with the issue, as the error states it's looking for App\Hashand I know it's not part of the Appnamespace, but the Illuminateone.

我是 PHP 命名空间的新手,我猜这可能与问题有关,因为错误指出它正在寻找App\Hash,我知道它不是App命名空间的一部分,而是命名空间的一部分Illuminate

Here's my code:

这是我的代码:

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['first_name', 'last_name', 'default_currency', 'default_timezone', 'default_location', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function setPasswordAttribute($value) {
        $this->attributes['password'] = Hash::make($value);
    }

}

Any help in figuring out the route cause of this, and any suggestions would be greatly appreciated!

任何有助于找出导致此问题的路线原因的帮助,以及任何建议,将不胜感激!

回答by V13Axel

You just need to import the \Hash class, or call it with \Hash::make(). Just doing Hashas you are looks within the namespace of the class you're calling it from. The Hashclass is part of the root namespace, or \.

您只需要导入 \Hash 类,或使用\Hash::make(). 只是在Hash您调用它的类的命名空间中按原样查找。的Hash类是根命名空间的一部分,或\

\Hashis a 'Facade' class, which just basically allows you to call static globally, from anywhere, without necessarily needing to import the original class. More information can be found on the documentation page for Facades.

\Hash是一个“门面”类,它基本上允许您从任何地方全局调用静态,而不必导入原始类。更多信息可以在Facades文档页面上找到。

回答by G-Rajendra

use Hash; after namespace App include use hash line it must work.

使用哈希;在命名空间 App 包含使用哈希行之后,它必须工作。