Laravel 5.8对成员函数create()的按需通知错误调用为空

Laravel 5.8对成员函数create()的按需通知错误调用为空,laravel,laravel-5,Laravel,Laravel 5,当我这样做时,用户会收到无误的电子邮件: Notification::send($user, new TicketNotification($details)); 但是,当我这样做时,用户也会收到一封电子邮件,但下面的屏幕截图中有一个错误 Notification::route('mail', 'email_of_non-db_user')->notify(new TicketNotification($details)); 你知道为什么吗?如何避免这个错误 我必须使用按需通知,因为

当我这样做时,用户会收到无误的电子邮件:

Notification::send($user, new TicketNotification($details));
但是,当我这样做时,用户也会收到一封电子邮件,但下面的屏幕截图中有一个错误

Notification::route('mail', 'email_of_non-db_user')->notify(new TicketNotification($details));

你知道为什么吗?如何避免这个错误

我必须使用按需通知,因为我需要向未存储为“用户”的用户发送通知。

我想试试这个

TicketNotification
中,通过
方法更新
,此方法仅用于发送至邮件

但u r也将通知保存到数据库中

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['mail'];
}

谢谢Jignesh,你的答案有效

抱歉,Thamer,我应该从一开始就发布整个代码

在此之前,它是:

return ['mail','database']; 
现在仅限于:

return ['mail'];  
然后,就没有错误了

这里是我的票证提示,它导致了错误:

<?php
namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class TicketNotification extends Notification
{
    use Queueable;

    private $details;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail','database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->subject($this->details['subject'])
                    ->greeting($this->details['title'])
                    ->line($this->details['body'])
                    ->line($this->details['links'])
                    ;
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        return [
            'order_id' => $this->details['order_id']
        ];
    }
}

将此添加到via方法中,以便对所有问题使用相同的通知:

public function via($notifiable)
{
    $availableChannels = [
        'mail' => 'mail',
        'database' => 'database',
        'slack' => 'slack',
        'telegram' => TelegramChannel::class
    ];

    $channels = [];
    foreach ($availableChannels AS $channel => $driver) {
        if ($notifiable->routeNotificationFor($channel)) {
            $channels[] = $driver;
        }
    }
    return $channels;
}

您现在可以使用点播通知或向用户发送通知,而无需为每个频道或点播等发送多个通知。

您确定$user->email不是空的吗?@Dan我想您也会发送通知(已保存通知到数据库)所以改成只发
邮件
@Thamer是的,我确信$user->email不是空的。它只是一个电子邮件地址,就像username@domain. 电子邮件地址来自laravel db中的同一个$user。@但我不明白如果数据库中存在此用户,为什么要使用Notification::route?@Thamer,因为稍后我还需要向未存储为“用户”的人发送通知。