0

I'm trying to get a users email and use it in my html code but I'm not sure how to do this, at the moment this is my code:

public function toArray($notifiable) 
{
    return [
        'data' => $this->user->name. ' commented on your post',
    ];
}

I am able to use the first line 'data'... in the following way:

{{ $notification->data['data'] }}

but I'm struggling to figure out how to use the email.

How would I do this?

At the moment I am using {{ $notification->data['data'] }} to get the name of the user that makes a comment on another user post.

When a user makes this comment, the posts owner receives a notification that looks like this:

how its supposed to look

but I need to add the name of the user that commented on the post before the 'commented on your post' text and I need the email to generate the profile picture.

I can use the email if I change the code above to:

'data' => $this->user->email

and I can use the name if I change the code to:

'data' => $this->user->name

but if I use one I cant use the other or at least i don't know how to.

so I'm asking how do I change it so that I can use both the email and name at the same time?

Rwd
  • 34,180
  • 6
  • 64
  • 78
es915
  • 137
  • 3
  • 11
  • You should use the [toMail()](https://laravel.com/docs/8.x/notifications#mail-notifications) method. The `toArray()` method is used for database or broadcasting. – Rwd Dec 21 '20 at 18:24
  • i only need the email to generate an image to add to a notification not to send an email – es915 Dec 21 '20 at 18:25
  • Oh, ok. Could you provide bit more information about what you're trying to achieve and how you're currently planning to achieve it. – Rwd Dec 21 '20 at 18:32
  • I have provided more info above. – es915 Dec 21 '20 at 21:08
  • Oh, I see what you mean now. When you said "email" I though you meant an actual email not an email address. The answer below should be what you need. – Rwd Dec 22 '20 at 08:08

1 Answers1

0

If you control that data you should be able to format it how you want:

public function toArray($notifiable) {
    return [
        'commenter' => [
            'name' => $this->user->name,
            'email' => $this->user->email,
        ],
        'message' => $this->user->name. ' commented on your post',
    ];
}

$notification->data['commenter']['email'] // email
$notification->data['message']            // message
lagbox
  • 48,571
  • 8
  • 72
  • 83
  • Now im getting: Undefined index: commenter – es915 Dec 23 '20 at 07:15
  • can you do a dump of `$notification->data` to see what it looks like – lagbox Dec 23 '20 at 07:18
  • do i do that in the html file or the toArray() function – es915 Dec 23 '20 at 07:36
  • i get: htmlspecialchars() expects parameter 1 to be string, array given if i do a dump on $notification->data – es915 Dec 23 '20 at 07:42
  • its working now, i just cleared the old notifications and creates new ones with the above code you provided and its working fine now thank you for the help. – es915 Dec 23 '20 at 07:52