doubairan4213 2018-11-02 04:55
浏览 59
已采纳

Laravel:跳过数据库通知

I am trying to send the explicit via Email only using the notification class where it is configured for both ['mail', 'database']

Notification::route('mail', $shopadmin->email)->notify(new ShopadminCreated($model));

But it still goes into toArray() method and generates error.

The reason is, I am using the same class to renotify using email but this time no database entry need to be created.

  • 写回答

1条回答 默认 最新

  • doupian6118 2018-11-04 22:36
    关注

    Solution:

    After stumbling upon the Documentation on Notification. I tried this block of code and implement with below modification:

    ShopadminCreated.php:

    ...
    public function via($notifiable)
    {
        return $notifiable->prefers_email ? ['mail'] : ['mail', 'database']; 
        //return ['mail', 'database'];
    }
    ...
    

    Controller:

    $data = $notification->data; // Array fetched from database
    $model = Admin::hydrate([$data])[0];
    $model->setAttribute('prefers_email', true);
    
    Notification::send($shopadmin, new ShopadminCreated($model));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部