Home > Back-end >  Attempt to read property "password" on null in Laravel
Attempt to read property "password" on null in Laravel

Time:02-28

I tried to use Hash and I kept getting this error:

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

I also tried importing Hash, but then I got another error.

Attempt to read property "password" on null

Here's my code from the UserController.php file:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Hash;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{
    //
    function login(Request $req)
    {
       $user= User::where(['email'=>$req->email])->first();
       if($user || Hash::check($req->password,$user->password))
       {
           return "Username or password is not matched";

       }
       else{
           $req->session()->put('user',$user);
           return redirect('/');
       }
    }
}

CodePudding user response:

When using ||, if the first operand evaluates to a truthy value, the evaluation is short-circuited, because the outcome will always be true as soon as any operand is true.

In your case, the $user variable may be null (if the email is not found in the database). Because null is a falsy value, the evaluation goes on to the next operand, where you try to read $user->password.

In your case, you want to evaluate the 2nd operand only if $user isn't null, so you should replace your || with an &&.

You can find the documentation for this behavior on the following page: https://www.php.net/manual/en/language.operators.logical.php

if ($user && Hash::check($req->password,$user->password))
//        ^^ <--- use "&&" instead of "||"

CodePudding user response:

Try to debug the $req and $user value. It happen because one of those variable has null value. So when you try to get password property it will return those error.

function login(Request $req)
{
   $user= User::where(['email'=>$req->email])->first();

   // Uncomment this following statement to check is those 2 variable has value or not
   // dd($req, $user); 

   if($user || Hash::check($req->password,$user->password))
   {
       return "Username or password is not matched";

   }
   else{
       $req->session()->put('user',$user);
       return redirect('/');
   }
}

CodePudding user response:

There is the error in expected logic. It should be

public function login(Request $request)
{
    $user = User::where([
            'email' => $request->email
        ])
        ->first();

    // correct logic would be read as 'if there is no user in database found by provided email 
    // OR 
    // if there is that kind of user but provided password is not one stored in database, return error'
    if (!$user || !Hash::check($request->password, $user?->password)) {
        return "Username or password is not matched";
    }

    // otherwise do what ever need to be done
    $request->session()->put('user', $user);
    return redirect('/');
}

If you are not using PHP 8 then substitute null safe operator with checking if object is not null before trying to execute method on variable

i.e.

// instead $user?->password
// it could be
if (!is_null($user)) { /** do something with $user->password */}

But it is minor thing, most important is to have correct logic similar to what I put above in code.

  • Related