PasswordResetController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Response;
use App\Http\Requests\PasswordResetRequest;
use Illuminate\Support\Str;
use App\Models\PasswordReset;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use App\Mail\PasswordResetMail;
class PasswordResetController extends Controller
{
public function reset_email(PasswordResetRequest $request){
$validated = $request->validated();
$token=Str::random(60);
PasswordReset::create([
'email'=>$validated['email'],
'token'=>$token,
]);
$user=User::where('email',$validated['email'])->get();
$mail=Mail::to($validated['email'])->send(new PasswordResetMail($user),['token'=>$token]);
if($mail){
return response([
'message'=>"Password reset email sent suceesfully",
],Response::HTTP_OK);
}
return response([
'message'=>"Failed to send passport reset email",
],Response::HTTP_UNAUTHORIZED);
}
}
PasswordResetMail.php
<?php
namespace App\Mail;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class PasswordResetMail extends Mailable
{
use Queueable, SerializesModels;
public $user;
/**
* The order instance.
*
*/
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($user)
{
$this->user=$user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('test');
}
}
test.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
@foreach($user as $u)
{{ $u->name }}
@endforeach
{{ $token }}
</body>
</html>
I am trying to send a password reset link into the mail so, whenever the user types their email into the email reset link page and presses send email reset link button an email will be sent along with the token. Here, I am trying to send a token into the email by writing above code but it is showing an undefined variable error on the test.blade.php page. What am I doing wrong I have no idea any suggestions, will be highly appreciated.
CodePudding user response:
PasswordResetMail.php construct should be like this
public function __construct($user,$token)
{
$this->user=$user;
$this->token=$token;
}
and don't forget to add this before construct:
public $token;
and in PasswordResetController class change mail variable exemple :
$mail=Mail::to($validated['email'])->send(new PasswordResetMail($user,$token));
i hope it was useful