duadpnld426905 2018-01-08 16:34
浏览 203
已采纳

Laravel On Demand通知错误“未定义的索引:名称”

Laravel 5.5.28

I'm using a Laravel's On Demand Notification to send out an email of a simple form submission to a central address. I have followed the laravel docs here

I have mailtrap set up in the env file.

The code in my controller:

use Notification // set at top of class
$submission = FormSubmission::create($request->all());

Notification::route('mail', 'test@test.org')
    ->notify(new FormSubmissionNotificiation($submission));

but get a whoops error. It fails on the vendor/laravel/framework/src/Illuminate/Mail/MailServiceProvider.php in this method

protected function setGlobalAddress($mailer, array $config, $type)
    {
        $address = Arr::get($config, $type);

        if (is_array($address) && isset($address['address'])) {
            $mailer->{'always'.Str::studly($type)}($address['address'], $address['name']);
        }
    }

where it's trying to find the $address['name'] index. But I don't have a name, and if I did, where do I put it?

Can't seem to figure it out, any help appreciated.

EDIT: I've tried this a different way. I added in a user to my database, and added the Notifiable trait onto the User model and attempted to send out a notification like

$user->notify(new FormSubmissionNotification($submission); and still got the same error.

  • 写回答

3条回答 默认 最新

  • dongzhuo3376 2018-01-09 09:36
    关注

    OK, I figured this out. Pretty stupid actually.

    I added in a MAIL_TO_ADDRESS variable to my .env file to hold the email address I wanted to send the notifications to, but didn't want to call the env file directly in the controller so set up a new array element in the config/mail.php file like this

     'to' => [
          'address' => env('MAIL_TO_ADDRESS')
      ],
    

    Then I planned to use that in the controller like this

    Notification::route('mail', config('mail.to.address'))
            ->notify(new FormSubmissionNotificiation($submission));
    

    But even while I was testing using a dummy email address in a string directly in the controller, it was using that to variable with the email address from the mail config file. Even though I wasn't referencing it anywhere.

    Once I deleted that whole array from the config it worked, and similarly, if I add in a name to that array, it works. That also stopped me using the standard $user->notify() as well and was always trying to use the email address from the env file rather than the User model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)