I'm trying to get the notifications for a user. But when i the method get() in my NotificationController start it's always return null. This this the actual code i'm using to debug this feature :
function get(){
$user = \App\User::find(12);
dd($user->notifications);
}
The route is with POST method, i use Postman to test it. I always get error 500 with null content. Do you know why ? Thank's in advance
[EDIT] This is the result of dd($user) for the get() method with POST request :
{
"id": 13,
"rang": 1,
"image": "https://www.drupal.org/files/profile_default.jpg",
"firstname": "Donald",
"lastname": "Torp",
"email": "[email protected]",
"created_at": null,
"updated_at": null
}
This is the content of my DB notifications table actually :
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
Result for return (dd($user->load('notifications'))); :
#relations: array:1 [
"notifications" => DatabaseNotificationCollection {#549
#items: []
}
]
Result if i change the notifiable_type from App\Idea to 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"
}
]
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;