2

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"
    }
]
5
  • Can you give us more info about the relationships. Commented Apr 15, 2018 at 10:49
  • @StefanoGroenland I added the notifiable attribute to User Class and this this my IdeaSelected.php file (Notification classe) : 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, ]; } } Commented Apr 15, 2018 at 10:53
  • You mean the notifiable trait? Commented Apr 15, 2018 at 10:56
  • Yes the trait, sorry. This one : class User extends Authenticatable { use Notifiable; Commented Apr 15, 2018 at 10:59
  • And if you dd($user) you get a user, correct? Commented Apr 15, 2018 at 11:00

2 Answers 2

1

Ah i see you are assigning the notifiable_type incorrectly it should say the User::class

When defining the notifiable model think to what you are sending an notification

Sign up to request clarification or add additional context in comments.

8 Comments

Should i replace "App\Idea" by "User::class" ??
Yes if thats the value you pass to notifiable_type
So the end result will be App\User
Ohhh !! Now i have a return ! Just look in the question this the result
Great! Didn't noticed it until you gave the return haha.. so for further ref. When defining the notifiable model think to what you are sending an notification
|
0

You should be able to grab the notificationse once you store them in your database.

Take a look at the docs here https://laravel.com/docs/5.6/notifications#database-notifications

13 Comments

Yes, i agree. The notify work fine ! All notifications are in the DB but i can't grab them. And yes i follew the official document, that's why i'm posting here :/
Yep, i used php artisan notifications:table
And you also ran php artisan migrate after that?
Yep i ran this command. The table has been created and i can fill it with notify()
And can you show me what dd($user) gives back? Please update your question with some markup
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.