Reputation: 1973
I need to pass user object and a password to Laravel mailable class. I've tried so many ways, nothings seems to work. Here's my latest attempt.
Password is passed into payload properly.
Controller
Mail::to($user)
->queue(new EmployeeCreated($password));
Mailable
class EmployeeCreated extends Mailable
{
use Queueable, SerializesModels;
public $password;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($password)
{
$this->password = $password;
$password = $password;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this
->subject('YOU ARE IN!')
->view('mail.employees.created')
->withPassword($password);
}
}
View
You got your account. Got to {{url('/login')}}. Use e-mail as username. Your password is {{$password}}.
Error message
ErrorException: Undefined variable: password in /var/www/html/storage/framework/views/4c04e63464617b1ec451525800e71aa14d7540d4.php:1
Upvotes: 1
Views: 2729
Reputation: 83
I think that the issue with in the build function Try,
public function build()
{
return $this
->subject('YOU ARE IN!')
->view('mail.employees.created')
->with('Password',$password);
}
Upvotes: 0
Reputation: 12574
You can explicitely pass variables in Mailable lyke this:
...
public function build()
{
return $this->view('emails.myview')->with(['variable' => $this->variable]);
}
...
Upvotes: 0
Reputation: 167
Try This :
class EmployeeCreated extends Mailable
{
use Queueable, SerializesModels;
public $user,$password;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($params)
{
$this->user = $params['user'];
$this->password = $params['password'];
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this
->subject('YOU ARE IN!')
->view('mail.employees.created');
}
}
Upvotes: 0
Reputation:
Pass them directly as type hinted constructor parameters, you're not limited on how many you can define. Set the access modifier to public:
public $user;
public $password;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(User $user, $password)
{
$this->user = $user;
$this->password = $password;
}
any public property defined on your mailable class will automatically be made available to the view.
Upvotes: 1