4
votes

I'm trying to use Hash in my global functions file.

I keep getting this error.

Class 'App\Http\Controllers\Hash' not found

where my file below is located at:

/app/Helpers/functions.php

<?php

    use App\Http\Controllers\Hash;

    // If old password matches password
    function checkOldPassword($oldPassword, $user) {
        if (Hash::check($oldPassword, $user->password)) {
            dd('a');
        }
        else {
            return back()->withErrors([
                'message' => 'Your old password is incorrect.'
            ]);
        }
    }
3

3 Answers

15
votes

import the hash class from use Illuminate\Support\Facades\Hash;

4
votes

Hash is a facade. It is working in your controller because has been imported correctly: use Hash; However, in other classes or files, you need to import it as mentioned or using it without making an inclusion but backslash:

<?php

// If old password matches password
function checkOldPassword($oldPassword, $user) {
    if (\Hash::check($oldPassword, $user->password)) {
        dd('a');
    }
    else {
        return back()->withErrors([
            'message' => 'Your old password is incorrect.'
        ]);
    }
}
1
votes

As the error helpfully points out, there is no class App\Http\Controllers\Hash unless you created one. Remove that line your code should behave normally.