继上一个问题之后,我设置了一个电子邮件控制器以将用户数据正确传递到视图。我现在正在尝试修改它,以便我可以传递一些自定义数据。我的控制器看起来像这样...
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]);
}
}
我正在这样发送电子邮件...
/* Create Data Array For Email */
$email_data = array(
'first_name'=>'John',
'last_name'=>'Doe',
'email'=>'[email protected]',
'password'=>'temp',
);
/* Send Email */
Mail::to($user->email)->send(new Welcome($email_data));
这是正确的吗?当我尝试使用此方法时,它似乎没有将数据传递到电子邮件模板。然后我如何在视图中访问这些数据?
这个方法你试过吗?
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);
}
}
然后在您创建数据数组的控制器中,
$data = [
'first_name'=>'John',
'last_name'=>'Doe',
'email'=>'[email protected]',
'password'=>'temp'
];
\Mail::to($user->email)->send(new Welcome($data));
请确保添加
use Mail;
use App\Mail\Welcome;
在您的控制器中。
您可以像这样访问视图中的数据
{{ $data['first_name'] }}
{{ $data['last_name'] }}
{{ $data['email'] }}
{{ $data['password'] }}
或
您也可以尝试Markdown 邮件 为此
您不需要这部分
->with(['email_data' => $this->email_data])
,因为如果该属性是公共的,您可以在视图中访问它。
并且您正在传递一个数组,因此您必须像这样访问值:
$email_data['email'] // ...
通过视图传递数据有两种方法。首先,可邮寄类中定义的任何公共防御都会自动通过视图。
<?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');
}
}
在刀片视图中
<div>
First Name: {{ $firstName }}
Last Name: {{ $lastName }}
Email: {{ $email }}
Password: {{ $password }}
</div>
对于具有受保护和私有属性的变量,可以使用 with 方法通过视图传递数据
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,
......
]);
}
}
在刀片视图中
<div>
First Name: {{ $firstName }}
Last Name: {{ $lastName }}
Email: {{ $email }}
Password: {{ $password }}
</div>
在 Laravel 11 中,
public function content(): Content
{
return new Content(
view: 'email.email',
with: [
'subject' => $this->subject,
'body' => $this->body,
],
);
}
您可以查看这里