简体   繁体   English

发送电子邮件时 Laravel 8 刀片模板中的“未定义变量”

[英]'Undefined variable' in Laravel 8 blade template on sending email

In my Laravel 8 project I try to send HTML e-mail, but get Undefined variable error.在我的 Laravel 8 项目中,我尝试发送 HTML 电子邮件,但收到Undefined variable错误。

I have this code in my controller:我的控制器中有此代码:

// here the $client has a model value
Mail::to($client)->send(new ClientCreated($client));

In my app/Mail/ClientCreated.php file:在我的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');
    }
}

Finally in my resources/views/emails/client/created.blade.php I have this code:最后在我的resources/views/emails/client/created.blade.php我有这个代码:

<p>Dear{{ $client->name }}!</p>

And I got this error message:我收到此错误消息:

Undefined variable: client (View: /home/vagrant/Code/myproject/laravel/resources/views/emails/client/created.blade.php)

I read the docs and search on the Stackoverflow, but not found any help.我阅读了文档并在 Stackoverflow 上搜索,但没有找到任何帮助。

Any idea what I made wrong?知道我做错了什么吗?

You should make $client public not private:您应该将$client公开而不是私有:

 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 Laravel 8.x 文档 - 邮件 - 编写邮件 - 查看数据 - 通过公共属性

The other method would be calling with :另一种方法将调用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." “如果您想在发送到模板之前自定义电子邮件数据的格式,您可以通过with方法手动将数据传递到视图。”

Laravel 8.x Docs - Mail - Writing Mailables - View Data - Via the with Method Laravel 8.x 文档 - 邮件 - 编写邮件 - 查看数据 - 通过with方法

If you do not want to change $client to public then use the second method.如果您不想将$client更改$client public则使用第二种方法。

如果你正确地将变量传递给了视图,但它仍然不起作用,请尝试在控制台中重新启动队列:

php artisan queue:restart

You should make $client public你应该公开 $client

public $client;

Once the data has been set to a public property, it will automatically be available in your view, so you may access it like you would access any other data in your Blade templates.一旦数据被设置为公共属性,它将自动在您的视图中可用,因此您可以像访问 Blade 模板中的任何其他数据一样访问它。 https://laravel.com/docs/8.x/mail#view-data https://laravel.com/docs/8.x/mail#view-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM