'Undefined variable' 在 Laravel 8 blade 模板中发送电子邮件
'Undefined variable' in Laravel 8 blade template on sending email
在我的 Laravel 8 项目中,我尝试发送 HTML e-mail,但收到 Undefined variable
错误。
我的控制器中有这段代码:
// here the $client has a model value
Mail::to($client)->send(new ClientCreated($client));
在我的 app/Mail/ClientCreated.php
文件中:
<?php
namespace App\Mail;
use App\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ClientCreated extends Mailable
{
use Queueable, SerializesModels;
private $client;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
// here the $this->client has a model value
return $this->view('emails.client.created');
}
}
终于在我的 resources/views/emails/client/created.blade.php
中有了这个代码:
<p>Dear{{ $client->name }}!</p>
我收到了这条错误信息:
Undefined variable: client (View: /home/vagrant/Code/myproject/laravel/resources/views/emails/client/created.blade.php)
我阅读了文档并在 Whosebug 上进行了搜索,但没有找到任何帮助。
知道我做错了什么吗?
你应该让 $client
public 不私有:
public $client;
"There are two ways you may make data available to your view. First, any public property defined on your mailable class will automatically be made available to the view"
Laravel 8.x Docs - Mail - Writing Mailables - View Data - Via Public Properties
另一种方法是调用 with
:
$this->view(...)->with('client', $this->client);
"If you would like to customize the format of your email's data before it is sent to the template, you may manually pass your data to the view via the with
method."
Laravel 8.x Docs - Mail - Writing Mailables - View Data - Via the with
Method
如果您不想将$client
更改为public
,则使用第二种方法。
你应该让$client public
public $client;
一旦数据被设置为 public 属性,它将自动出现在您的视图中,因此您可以像访问 Blade 模板中的任何其他数据一样访问它。
https://laravel.com/docs/8.x/mail#view-data
如果你正确的将变量传给了视图,但是还是不行,尝试在控制台重启队列:
php artisan queue:restart
在我的 Laravel 8 项目中,我尝试发送 HTML e-mail,但收到 Undefined variable
错误。
我的控制器中有这段代码:
// here the $client has a model value
Mail::to($client)->send(new ClientCreated($client));
在我的 app/Mail/ClientCreated.php
文件中:
<?php
namespace App\Mail;
use App\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ClientCreated extends Mailable
{
use Queueable, SerializesModels;
private $client;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
// here the $this->client has a model value
return $this->view('emails.client.created');
}
}
终于在我的 resources/views/emails/client/created.blade.php
中有了这个代码:
<p>Dear{{ $client->name }}!</p>
我收到了这条错误信息:
Undefined variable: client (View: /home/vagrant/Code/myproject/laravel/resources/views/emails/client/created.blade.php)
我阅读了文档并在 Whosebug 上进行了搜索,但没有找到任何帮助。
知道我做错了什么吗?
你应该让 $client
public 不私有:
public $client;
"There are two ways you may make data available to your view. First, any public property defined on your mailable class will automatically be made available to the view"
Laravel 8.x Docs - Mail - Writing Mailables - View Data - Via Public Properties
另一种方法是调用 with
:
$this->view(...)->with('client', $this->client);
"If you would like to customize the format of your email's data before it is sent to the template, you may manually pass your data to the view via the
with
method."
Laravel 8.x Docs - Mail - Writing Mailables - View Data - Via the with
Method
如果您不想将$client
更改为public
,则使用第二种方法。
你应该让$client public
public $client;
一旦数据被设置为 public 属性,它将自动出现在您的视图中,因此您可以像访问 Blade 模板中的任何其他数据一样访问它。 https://laravel.com/docs/8.x/mail#view-data
如果你正确的将变量传给了视图,但是还是不行,尝试在控制台重启队列:
php artisan queue:restart