"Undefined variable $token", - laravel

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.

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

Related

GET http://localhost:6001/socket.io/?EIO=3&transport=polling&t=MloS95c net::ERR_CONNECTION_REFUSED

I am very new laravel broadcasting. I am working with redis, socket.io and laravel echo. When i reflesh the page this is write on console
GET http://localhost:6001/socket.io/?EIO=3&transport=polling&t=MloS95c
net::ERR_CONNECTION_REFUSED
My Test Event:
namespace App\Events;
use Illuminate\Broadcasting\Channel; use Illuminate\Queue\SerializesModels; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class TestEvent {
use SerializesModels;
public $message;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel ('Message');
}
}
My head :
<script src="http://{{ Request::getHost() }}:6001/socket.io/socket.io.js"></script>
<meta name="csrf-token" content="{{ csrf_token() }}">
My Controller:
public function dev(){
event(new TestEvent("Hello"));
return view('home');
}
My Js file:
window.Echo.channel(`Message`)
.listen('TestEvent', (data) => {
console.log(data);
});
You are getting this error as the laravel echo server is not started. Inside your package.json file add under script the following line.
"scripts": {
"start": "laravel-echo-server start",
```
},
Now you need to go to the console and run npm start command into your project root directory. It will start the lavavel echo server and the error will be gone.

Laravel - Pass custom data to email view

Following on from a previous question, I have an email controller set up to correctly pass user data to the view. I am now trying to modify it so I can pass some custom data instead. My controller looks like this...
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class Welcome extends Mailable
{
use Queueable, SerializesModels;
public $email_data;
public function __construct($email_data)
{
$this->email_data = $email_data;
}
public function build()
{
return $this->view('emails.welcome')->with(['email_data' => $this->email_data]);
}
}
And I am sending the email like this...
/* Create Data Array For Email */
$email_data = array(
'first_name'=>'John',
'last_name'=>'Doe',
'email'=>'john#doe.com',
'password'=>'temp',
);
/* Send Email */
Mail::to($user->email)->send(new Welcome($email_data));
Is this correct? When I try using this method it does not seem to be passing the data through to the email template. How can I then access this data within the view?
Have you tried this way ?
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class Welcome extends Mailable
{
use Queueable, SerializesModels;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function build()
{
return $this->view('emails.welcome')->with('data', $this->data);
}
}
and then in your controller from where you are creating your array of data,
$data = [
'first_name'=>'John',
'last_name'=>'Doe',
'email'=>'john#doe.com',
'password'=>'temp'
];
\Mail::to($user->email)->send(new Welcome($data));
Please make sure that you add
use Mail;
use App\Mail\Welcome;
in your controller.
You can access the data in your view like this
{{ $data['first_name'] }}
{{ $data['last_name'] }}
{{ $data['email'] }}
{{ $data['password'] }}
OR
You can also try Markdown mails for this
You don't need this part ->with(['email_data' => $this->email_data]) because if the property is public you can access it in the view.
And you are passing an array so you have to access the values like this :
$email_data['email'] // ...
There are two ways to pass data through the view. First, any public defenses defined in the mailable class pass automatically through the view.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class Welcome extends Mailable
{
use Queueable, SerializesModels;
public $firstName;
public $lastName;
public $email;
public $password;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($firstName, $lastName, $email, $password)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->password = $password;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.orders');
}
}
In Blade view
<div>
First Name: {{ $firstName }}
Last Name: {{ $lastName }}
Email: {{ $email }}
Password: {{ $password }}
</div>
For variables with protected and private properties, it is possible to pass data through a view with the with method
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class Welcome extends Mailable
{
use Queueable, SerializesModels;
protected $firstName;
protected $lastName;
protected $email;
protected $password;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($firstName, $lastName, $email, $password)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->password = $password;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.orders')->with([
'first_name'=> $this->firstName,
......
]);
}
}
In Blade view
<div>
First Name: {{ $firstName }}
Last Name: {{ $lastName }}
Email: {{ $email }}
Password: {{ $password }}
</div>

Laravel Mail view not returning passed data

I am trying to send email using Laravel default Mail facade:-
Mail::to($user->email)->send(New NotifyUserExpiring($diff,$Sub->user));
In Mail\NotifyUserExpiring :-
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class NotifyUserExpiring extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
protected $user;
protected $diff;
public function __construct($diff,$user)
{
$this->user = $user;
$this->diff = $diff;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('web.mail.NotifyUserExpiring')->with('user',$this->user)->with('diff',$this->diff);
}
}
And in web.mail.NotifyUserExpiring View file i am using the blade way of printing variable :-
Hello {{ $user->first_name }}
Message Here
When i check my mail inbox , i am reciving email with exact {{ $user->first_name }}
Hello {{ $user->first_name }}
Message Here
I am expecting that when i insert {{ $user->first_name }} in mail view file , it should return user first name.
<div>
Hello {{ $user->first_name }}
Message body test
</div>
Make sure your mail view file name is suffixed as .blade.php so that it goes through the Blade rendering engine.
The with() method you can pass as an array, see the documentation:
return $this->view('web.mail.NotifyUserExpiring')->with([
'user',$this->user,
'diff',$this->diff
]);

Render a view as response of Event Broadcasting

Hi there i'm under development of a Laravel webapp that use events broacasting troug redis, and socket.io. All is working fine, but i tring to return a rendered view as response of Event.
Mi event is something like this:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class EventName implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public $data;
public function __construct()
{
$this->data = array(
'power'=> 'Funziona',
'view'=> view('dashboard.partials.messages')->render()
);
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return ['test-channel'];
}
}
and i use this code to render response on the page:
<script src="/frontend/socket.io.js"></script>
<script>
var socket = io('http://1clickfashion.com:3002');
socket.on("test-channel:App\\Events\\EventName", function(message){
// increase the power everytime we load test route
alert(message.data.power);
$('#messages').html('');
$('#messages').html(message.data.view);
});
</script>
The "power" alert is displayed correctly but the view don't work as well. In another view i'm use the view as return response()->json($view) and works perfectly... Someone have similar issues?
For anyone that struggle in this issue, solved by passing the view as variable rendered in Controller. Like this.
In Controller:
$view = view('dashboard.partials.messages')->with('post', $post_c)->render();
event(new \App\Events\Post($view));
in event:
public $data;
public function __construct($view)
{
$this->data = array(
'view'=> $view
);
}

Passing a collection to a mailable| Laravel 5.4

I am trying to get a mailable setup which has a collection of files. Mail controller looks like:
<?php
namespace App\Mail;
use App\Document;
use App\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
class OrderComplete extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $order;
public $documents;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(User $user, Order $order, Document $document)
{
//
$this->user = $user;
$this->order = $order;
$this->documents = $document;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('emails.customers.complete');
}
}
Controller calling the mailable looks like;
use App\Document;
// other code...
$documents = Document::where('order_id', $orderId)
->where('product', 'like', '%response')
->get();
Mail::to($customer)
->send(new OrderComplete($customer, $order, $documents));
But I keep getting this error:
Type error: Argument 3 passed to App\Mail\OrderComplete::__construct() must be an instance of App\Document, instance of Illuminate\Database\Eloquent\Collection given, called in /Users/ap/sites/propair/app/Http/Controllers/OrderController.php on line 253
I'm pretty confused as I thought this should work?
thanks
This function declaration:
public function __construct(..., Document $document)
means PHP will enforce that $document is an instance of App\Document.
If you want to pass it a collection instead, you'll need to do:
public function __construct(..., \Illuminate\Database\Eloquent\Collection $documents)

Resources