I have one table in which I store password reset tokens. There are 4 fields in that table. They are user_id
, token
, created_at
and updated_at
.
What I want to do is, Check if user already has password reset token or not. If a user already has password reset token, I want to send an email with the same token. If a user does not have a token, I want to create token and then send the email.
Problem is, I can send the email if token is already created. But, I am getting the error Trying to get property of non-object (View: /var/www/html/project_name/resources/views/emails/passwordResetLink.blade.php)
.
Why can't I access the password reset token with $user->passwordResetLink->token
directly after creating the record in the database? Here is my code.
$user = User::findByUsername(request('username'));
if($user)
{
if(count($user->passwordResetLink))
{
$user->passwordResetLink->save();
}
else
{
$token = md5(str_random(16));
$record = new PasswordReset;
$record->user_id = $user->id;
$record->token = $token;
$record->save();
}
Mail::to($user)->send(New \App\Mail\PasswordResetLink($user));
return 1;
}
I am trying to access user's password reset token using $user->passwordResetLink->token
. It's working fine if the record is already created but not working if the record does not exist.
PS: I am using save()
because touch()
isn't working because I don't have id
field in my password_resets
table in the database. let me know what's wrong with the code. I know save()
won't work as I am not updating anything!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire