Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot access custom data #112

Open
hktang opened this issue Apr 20, 2023 · 3 comments
Open

Cannot access custom data #112

hktang opened this issue Apr 20, 2023 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@hktang
Copy link

hktang commented Apr 20, 2023

I am trying to pass custom data App\Review and App\Reviewer into the notification message.

However, it seems the email message cannot access those values, resulting in an error. Both $review and $reviewer are null.

This is how the notification is set up. Where did I get it wrong?

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class ReviewDueSoon extends Notification
{
    use Queueable;

    public $review;
    public $reviewer;

    /**
     * Create a new notification instance.
     */
    public function __construct($review, $reviewer)
    {
        $this->review = $review;
        $this->reviewer = $reviewer;
        ...
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        $mailMessage = new MailMessage();
        ...

        // $this->review is null
        // $this->reviewer is null

        return $mailMessage;
    }

  }
@hktang
Copy link
Author

hktang commented Apr 20, 2023

I think I figured out the issue: For the scheduled notifications to work, the notification initializer must take exactly one parameter.

So, instead of:

$reviewer->notifyAt(new ReviewDueSoon($review, $reviewer), $when, $meta);

This should be used:

$reviewer->notifyAt(new ReviewDueSoon([
        "review" => $review, 
        "reviewer" => $reviewer
    ]),
    $when,
    $meta
);

Then, in the notification constructor, we set up the notification like this:

    public function __construct($review, $params)
    {
        $this->review = $params["review"];
        $this->reviewer = $params["reviewer"];
        ...
    }

@chatisk
Copy link

chatisk commented Apr 28, 2023

Thanks @hktang !

@atymic
Copy link
Collaborator

atymic commented Mar 20, 2024

Feel free to PR a fix to this issue, as it would be good to support multi params

@atymic atymic added bug Something isn't working help wanted Extra attention is needed labels Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants