无法将变量传递给Mail :: queue()消息

时间:2015-01-09 05:45:11

标签: laravel-4

使用Laravel 4.2,我尝试使用以下代码对邮件进行排队:

public function sendEmailNotices($type, $user, array $data = [])
{
    $type = strtoupper($type);
    $emails = [];

    if (Config::get("emailer.$type.email_others"))
        $emails = Config::get("emailer.$type.email_others");

    if (Config::get("emailer.$type.email_user"))
        $emails += [$user->email];

    if (!empty($emails)) {
        foreach ($emails as $email) {
            Mail::queue('emails.' . strtolower($type), array_merge((array)$data, ['user' => $user, 'is_user' => $email == $user->email]), function ($message) use ($email, $user, $type) {
                $message->to($email, $user->first_name . ' ' . $user->last_name)->subject(Config::get("emailer.$type.subject"));
            });
        }
    }
}

在我正在测试的模板中,我有:

<div>
    Welcome {{ $user->first_name }} {{ $user->last_name }} [{{ $user->email }}].
</div>

这总是会产生错误:

Trying to get property of non-object

我认为User对象刚刚没有在队列中存活,所以我尝试将其更改为将'first_name'作为变量传递。当我这样做时,我得到了错误:

Undefined variable: first_name

排队时为什么没有通过模板?当我做Mail::send()时,工作得很好。

1 个答案:

答案 0 :(得分:1)

Mail::Send原样传递PHP个变量。

Mail::Queue使用队列,将所有php变量转换为数组表示法。

进入兔子洞,我们走了:

路径:vendor \ laravel \ framework \ src \ Illuminate \ Mail \ Mailer.php 163

public function queue($view, array $data, $callback, $queue = null)
{
    $callback = $this->buildQueueCallable($callback);

    return $this->queue->push('mailer@handleQueuedMessage', compact('view', 'data', 'callback'), $queue);
}