未定义的变量-Laravel通过电子邮件发送 [英] Undefined variable - laravel emailing

查看:98
本文介绍了未定义的变量-Laravel通过电子邮件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用laravel发送电子邮件.这适用于服务器功能,但与此同时,我遇到了一个奇怪的错误.

I'm trying to send an email using laravel. This worked for serveral functions but with this one I'm getting a weird error.

这是我正在使用的代码

    $offerte = DB::table('offertes')
        ->select('*')
        ->where('id', '=', $offerte_id)
        ->get();

    Mail::send('emails.offerte_reactie', ['offerte' => $offerte, 'user' => $user, 'message_text' => $message_text], function ($message) use ($user)
    {
        $message->from($user->email, 'Foodtruckbestellen.be');
        $message->to($offerte->email);
        $message->subject('Reactie offerte Foodtruckbestellen.be');
    });

但是,当我想发送邮件时,却收到错误消息

However when I want to send the mail I get the error

未定义的变量:offerte

Undefined variable: offerte

在PHPSTORM中,行 $ message-> to($ offerte-> email)确实用红色下划线,并且我没有在电子邮件中使用该变量.所以我可以确定那是我的错误所在,只是找不到解决方法.

The line $message->to($offerte->email); is indeed underlined in red in PHPSTORM and I don't use the variable in the email. so I know for sure that's where my error is I just can't find a fix for it.

推荐答案

您需要将$offerte变量传递给闭包,因为该变量未在闭包范围内定义.

You need to pass the $offerte variable to closure as it's not defined within the closure scope.

只需像使用$user一样使用$offerte变量.

Just use the $offertevariable as you do with the $user.

Mail::send('emails.offerte_reactie', ['offerte' => $offerte, 'user' => $user, 'message_text' => $message_text], function ($message) use ($user, $offerte)
{
    $message->from($user->email, 'Foodtruckbestellen.be');
    $message->to($offerte->email);
    $message->subject('Reactie offerte Foodtruckbestellen.be');
});

此外,如果我是您,我会使用 compact 用于传递给send函数的第二个参数以编写更简洁代码的数组.

Furthermore, if I were you, I'd use compact for the array that you pass to the send function's second argument to write more clean code.

Mail::send('emails.offerte_reactie', compact('offerte', 'user', 'message_text'), function ($message) use ($user, $offerte)
{
    $message->from($user->email, 'Foodtruckbestellen.be');
    $message->to($offerte->email);
    $message->subject('Reactie offerte Foodtruckbestellen.be');
});

这篇关于未定义的变量-Laravel通过电子邮件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆