【问题标题】:Laravel 7.x Illegal offset typeLaravel 7.x 非法偏移类型
【发布时间】:2020-09-24 23:35:14
【问题描述】:

WelcomeMail.php;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('email.register-mail');
    }
}

RegisterController.php;

protected function create(array $data)
    {
        $user =User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),

        ]);



        $body = [];
        $mailData = array('body'=>$body);

        Mail::send('email.register-mail',$mailData, function ($message) use ($user){
            $message->from(env('MAIL_USERNAME'), 'EksikParça.');
            $message->subject('Hosgeldiniz!');
            $message->to(new WelcomeMail($user));
        });



        return $user;
    }
}

这是我为用户注册时发送的电子邮件编写的代码。但 我收到非法偏移类型错误。可能是什么原因造成的?

【问题讨论】:

  • 您应该提供完整的确切错误和正在使用的视图
  • 我也认为你没有正确使用$message->to(...) .. 就像你试图将旧的发送邮件方式和新的可邮寄方式混合在一起

标签: php laravel error-handling laravel-7


【解决方案1】:

Mail 中的 to() 方法需要接收者。

您可以将用户对象或电子邮件地址传递给它。

所以尝试这样做:

Mail::to($user)
    ->send(new WelcomeMail($user));

您可以在 Mail 类中定义主题和从右侧。在此处阅读更多信息:https://laravel.com/docs/8.x/mail#sending-mail

【讨论】:

    猜你喜欢
    • 2019-05-03
    • 2020-10-21
    • 2019-07-16
    • 1970-01-01
    • 2018-01-28
    • 2011-01-12
    • 2013-03-24
    • 2020-11-26
    • 2021-05-29
    相关资源
    最近更新 更多