In my UserController I have a function
public function userFollow($id)
{
$authuser = Auth::user();
$authuser->follow($id);
//mail goes to the followiee ($id)
$followiee = User::where('id', $id)->first();
$to = $followiee->email;
Mail::to($to)->send(new MyMail);
return redirect()->back()->with('message', 'Following User');
}
I have also created a Mailable MyMail
class MyMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.welcome');
}
}
Inside my welcome email I need to access some variables like $to which is defined in the UserController
I tried the following in MyMail Mailable:
public function build()
{
return $this->view('emails.welcome',['to' => $to]);
}
But I am getting Undefined variable: to
How to pass variables from Controller to Mailables?
Update:
What I have tried so far:
UserController
Mail::to($to)->send(new MyMail($to));
MyMail
public $to;
public function __construct($to)
{
$this->to = $to;
}
public function build()
{
return $this->view('emails.welcome');
}
Welcome.blade.php
{{ $to }}
Error:
FatalErrorException in Mailable.php line 442:
[] operator not supported for strings