【问题标题】:Laravel $user->notifications return nullLaravel $user-> 通知返回 null
【发布时间】:2018-04-15 19:27:33
【问题描述】:

我正在尝试获取用户的通知。但是当我在 NotificationController 中的方法 get() 开始时,它总是返回 null。这是我用来调试此功能的实际代码:

 function get(){

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

    dd($user->notifications);

}

路由是 POST 方法,我用 Postman 来测试。我总是收到带有空内容的错误 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"
    }
]

【问题讨论】:

  • 你能给我们提供更多关于这些关系的信息吗?
  • @StefanoGroenland 我将通知属性添加到用户类,这是我的 IdeaSelected.php 文件(通知类):class IdeaSelected extends Notification { use Queueable; public $idea; public function __construct(Idea $idea) { $this->idea = $idea; } public function via($notifiable) { return ['database']; } public function toDatabase($notifiable) { return [ 'idea_id' => $this->idea->id, 'idea_name' => $this->idea->name, ]; } }
  • 你的意思是通知特征?
  • 是的特性,对不起。这个:class User extends Authenticatable { use Notifiable;
  • 如果你 dd($user) 你得到一个用户,对吗?

标签: laravel notifications


【解决方案1】:

啊,我看到你错误地分配了 notifiable_type 它应该说 User::class

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

【讨论】:

  • 我应该用“User::class”替换“App\Idea”吗??
  • 是的,如果这是您传递给 notifiable_type 的值
  • 所以最终结果将是 App\User
  • 哦!现在我有回报了!看看这个问题的结果
  • 太棒了!直到你给了回报才注意到它哈哈..所以请进一步参考。在定义通知模型时,请考虑您发送通知的内容
【解决方案2】:

一旦将通知存储在数据库中,您应该能够获取它们。

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

【讨论】:

  • 是的,我同意。通知工作正常!所有通知都在数据库中,但我无法获取它们。是的,我关注了官方文件,这就是我在这里发帖的原因:/
  • 是的,我用过php artisan notifications:table
  • 然后你还跑了 php artisan migrate?
  • 是的,我运行了这个命令。表已创建,我可以用 notify() 填充它
  • 你能告诉我 dd($user) 回馈什么吗?请用一些标记更新您的问题
猜你喜欢
  • 1970-01-01
  • 2017-03-17
  • 2023-02-04
  • 2019-09-20
  • 2018-03-08
  • 2016-05-13
  • 2021-01-02
  • 2020-08-14
  • 2017-06-29
相关资源
最近更新 更多