Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

php - How do i use a second value from my toArray function in laravel when creating a notification

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...