关系没有传递给通知? [英] Relationship not being passed to notification?

查看:59
本文介绍了关系没有传递给通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个通知,告知我要将模型传递给:

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));

问题在于,当通知打印日志(Log::info($this->message);)时,topic关系没有显示.

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

但是,我发现如果将nofitication类中的toArray()函数更改为此,它可以很好地打印出来:

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?

推荐答案

您的通知设置为排队,并且您要扩展的Notification类使用SerializesModels特性.当具有SerializesModels特征的对象被序列化以放入队列时,该对象上包含的任何模型(例如,您的消息)将仅替换为该模型的ID(消息ID).当队列工作程序反序列化您的通知以对其进行处理时,它将使用该消息ID从数据库中重新检索该消息.不幸的是,发生这种情况时,不包含任何关系.

Your notification is set to queue, and the Notification class you're extending uses the SerializesModels trait. When an object with the SerializesModels trait is serialized to be put on the queue, any Models contained on that object (e.g. your message) are replaced with just the id of that model (the message id). When the queue worker unserializes your notification to process it, it will use that message id to re-retrieve the message from the database. Unfortunately, when this happens, no relationships are included.

因此,即使您的消息在序列化时已加载了主题关系,但在队列工作程序处理通知时也不会加载主题关系.如您所见,如果您在通知中需要该主题,则需要重新加载它.

So, even though your message had the topic relationship loaded when it was serialized, it will not have the topic relationship loaded when the queue worker processes the notification. If you need the topic inside of your notification, you will need to reload it, as you have seen.

您可以在此处的文档中了解有关此内容的更多信息.相关部分引述如下:

You can read more about this in the documentation here. The relevant part is quoted below:

在此示例中,请注意,我们能够将Eloquent模型直接传递到排队作业的构造函数中.由于作业正在使用SerializesModels特性,因此在处理作业时,雄辩的模型将正常地序列化和反序列化.如果您排队的作业在其构造函数中接受Eloquent模型,则仅该模型的标识符将被序列化到队列中.实际处理完作业后,队列系统将自动从数据库中重新检索完整的模型实例.所有这些对您的应用程序都是完全透明的,并且可以防止序列化完整的Eloquent模型实例而引起的问题.

In this example, note that we were able to pass an Eloquent model directly into the queued job's constructor. Because of the SerializesModels trait that the job is using, Eloquent models will be gracefully serialized and unserialized when the job is processing. If your queued job accepts an Eloquent model in its constructor, only the identifier for the model will be serialized onto the queue. When the job is actually handled, the queue system will automatically re-retrieve the full model instance from the database. It's all totally transparent to your application and prevents issues that can arise from serializing full Eloquent model instances.

这篇关于关系没有传递给通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆