Laravel 8: Checking user's data with database table does not work properly - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Friday, June 4, 2021

Laravel 8: Checking user's data with database table does not work properly

I have created a two factor authentication system, and it redirects user to token.blade.php where he must enters the token that is going to be sent to his phone number and also stored at active_codes table.

Now I want to check if the user has entered the same token code that was stored at active_codes table which looks like this:

enter image description here

And then at the Controller, I tried this:

public function submit(Request $request)
    {
        $data = $request->validate([
            'token' => 'required|min:6'
        ]);

        if($data['token'] === auth()->user()->activeCode()->code){
            dd('same');
        }
    }

But when I enter the same token, I get this error:

ErrorException Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$code

so my question is how to check if the requested token code of user, is as same as the token code which is stored on the DB ?

I would really appreciate any idea or suggestion from you guys...

Thanks in advance.

Note: The relationship between User and ActiveCode Models is OneToMany.

User.php:

public function activeCode()
    {
        return $this->hasMany(ActiveCode::class);
    }

ActiveCode.php:

public function user()
    {
        return $this->belongsTo(User::class);
    }


source https://stackoverflow.com/questions/67827117/laravel-8-checking-users-data-with-database-table-does-not-work-properly

No comments:

Post a Comment