在上一个问题之后,我设置了一个电子邮件控制器,以将用户数据正确传递到视图。我现在正尝试对其进行修改,以便可以传递一些自定义数据。我的控制器看起来像这样...
sideEffects: 'cacheable'
我正在这样发送电子邮件...
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]);
}
}
这是正确的吗?当我尝试使用此方法时,似乎没有将数据传递到电子邮件模板。然后如何在视图中访问此数据?
答案 0 :(得分:2)
您尝试过这种方式吗?
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'=>'john@doe.com',
'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'] }}
OR
您也可以尝试Markdown mails
答案 1 :(得分:1)
您不需要->with(['email_data' => $this->email_data])
这部分,因为如果该属性是公共的,则可以在视图中访问它。
您正在传递一个数组,因此必须访问这样的值:
$email_data['email'] // ...
答案 2 :(得分:1)
有两种方法可以通过视图传递数据。首先,在可邮件类中定义的所有公共防御都将自动通过视图。
<?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>