dimanche 28 mai 2017

Relationship not being passed to notification?

I created a notification that I am passing a model to:

class NewMessage extends Notification implements ShouldQueue
{
    use Queueable;

    protected $message;

    public function __construct(Message $message)
    {
        $this->message = $message;
    }

    public function via()
    {
        return ['database'];
    }

    public function toArray()
    {
        Log::info($this->message);

        return [
            'message_id' => $this->message->id,
        ];
    }
}

And this is how I call the notification:

$message = Message::where('user_id', Auth::user()->id)
    ->where('message_id', $message_id)
    ->with('topic')
    ->get();

$user->notify(new NewMessage($message));

The problem is that when the notification prints the log (Log::info($this->message);), the topic relationship doesn't show up.

However, I found that if I change the toArray() function in the nofitication class to this, it prints out fine:

public function toArray()
{
    $this->message->topic;

    Log::info($this->message);

    return [
        'message_id' => $this->message->id,
    ];
}

Why? How do I fix this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire