Laravel $ user->通知返回null

时间:2018-04-15 10:41:41

标签: laravel notifications

我正在尝试为用户收到通知。但是当我在NotificationController中使用方法get()时,它始终返回null。这是我用于调试此功能的实际代码:

 function get(){

    $user = \App\User::find(12);

    dd($user->notifications);

}

路线是用POST方法,我使用Postman来测试它。我总是得到带有null内容的错误500。 你知道为什么吗 ? 提前谢谢

[编辑] 这是带有POST请求的get()方法的dd($ user)的结果:

{ 
"id": 13, 
"rang": 1, 
"image": "https://www.drupal.org/files/profile_default.jpg", 
"firstname": "Donald", 
"lastname": "Torp", 
"email": "ashlynn.mcclure@pacocha.com", 
"created_at": null, 
"updated_at": null 
}

这实际上是我的数据库通知表的内容:

ID : e7f54269-a5ff-4109-9b8a-c2328fb6cf98   
Type : App\Notifications\IdeaSelected   
notifiable_type : App\Idea  
notifiable_id : 13  
data : {"idea":{"id":39,"name":"okeefe","description":"Sunt soluta et eum. Vitae sint adipisci et excepturi eos est exercitationem velit. Mollitia hic aut aperiam cum incidunt dolorem sed. Ut blanditiis eos temporibus hic illum quis.","image":"https:\/\/lorempixel.com\/400\/280\/?98265","user":13,"created_at":null,"updated_at":null}}     
read_at :   
created_at : 2018-04-14 00:57:37

return (dd($user->load('notifications')));的结果:

#relations: array:1 [
    "notifications" => DatabaseNotificationCollection {#549
      #items: []
    }
  ]  

如果我将notifiable_type从App \ Idea更改为App \ User:

[
    {
        "id": "e7f54269-a5ff-4109-9b8a-c2328fb6cf98",
        "type": "User::class",
        "notifiable_type": "App\\User",
        "notifiable_id": 13,
        "data": {
            "idea": {
                "id": 39,
                "name": "okeefe",
                "description": "Sunt soluta et eum. Vitae sint adipisci et excepturi eos est exercitationem velit. Mollitia hic aut aperiam cum incidunt dolorem sed. Ut blanditiis eos temporibus hic illum quis.",
                "image": "https://lorempixel.com/400/280/?98265",
                "user": 13,
                "created_at": null,
                "updated_at": null
            }
        },
        "read_at": null,
        "created_at": "2018-04-14 00:57:37",
        "updated_at": "2018-04-14 00:57:37"
    }
]

2 个答案:

答案 0 :(得分:0)

将数据存储到数据库后,您应该可以获取通知。

在这里查看文档https://laravel.com/docs/5.6/notifications#database-notifications

答案 1 :(得分:0)

啊,我发现你错误地指定了notifiable_type,应该说是User :: class

定义可通知模型时,请考虑发送通知的内容