首页 > 解决方案 > 如何解决 compact() 函数中的未定义变量错误?

问题描述

在此处输入图像描述

我想发送包含一些信息的电子邮件。这就是我compact()在函数中使用函数的原因view()。但我得到一个错误“未定义的变量”:

public $message_to_send="";
public $first_name_to_send="";

public function __construct($first_name,$message) {
    $this->message_to_send=$message;
    $this->first_name_to_send=$first_name;
}

public function build(){
    return $this->view('email/contactmessageemail',compact('message_to_send','first_name_to_send'));
}

错误信息是:

compact():未定义变量:message_to_send

标签: phplaravel

解决方案


尝试将其编辑为

view('email.contactmessageemail',[
   'message_to_send' => $this->message_to_send,
   'first_name_to_send' => $this->first_name_to_send
]);

compact在您的情况下不起作用,因为您要传入的变量不是在方法范围内本地定义的,它被声明为公共属性


推荐阅读