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

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.'
            ]);
        }
    }

2 answers

  • answered 2018-03-13 21:21 Andrew Gillis

    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.

  • answered 2018-03-14 17:55 manix

    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.'
            ]);
        }
    }