AliBaba
AliBaba

Reputation: 67

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

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

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 Illuminate namespace classes - and from what I gather Hash:: should be globally available?

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\Hash and I know it's not part of the App namespace, but the Illuminate one.

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!

Upvotes: 3

Views: 9290

Answers (2)

G-Rajendra
G-Rajendra

Reputation: 215

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

Upvotes: -1

V13Axel
V13Axel

Reputation: 838

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

\Hash is 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.

Upvotes: 12

Related Questions