Laravel 5.6-无法将变量传递到电子邮件视图

时间:2018-08-02 14:43:40

标签: laravel laravel-5

我已经在Laravel 5.6应用程序中设置了一个邮件程序,但是正在努力将变量传递给视图,我的welcome.php控制器看起来像这样。.

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 $user;

    public function __construct()
    {
        $this->user = $user;
    }

    public function build()
    {
        return $this->view('emails.welcome');
    }
}

任何我的看法都是这样。

Your registered email-id is {{$user->email}}

这样创建用户后,我正在呼叫电子邮件控制器...

$user->save();
Mail::to($user->email)->send(new Welcome($user));

我收到一条错误消息,告诉我未定义的变量:用户

我要去哪里错了?

2 个答案:

答案 0 :(得分:2)

您的构造函数不接受任何变量...

public function __construct()
{
    $this->user = $user;
}

将其更改为:

public function __construct($user)
{
    $this->user = $user;
}

或者您可以像这样键入提示:

use App\User;


...


public function __construct(User $user)
{
    $this->user = $user;
}

答案 1 :(得分:1)

编辑旅游邮件文件

public function __construct($user)
{
    $this->user = $user;
}

public function build()
{
    return $this->view('emails.welcome')->with(['user' => $this->user]);
}