关系没有传递给通知?
Relationship not being passed to notification?
我创建了一个通知,我正在将模型传递给:
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,
];
}
}
这就是我调用通知的方式:
$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
关系没有显示。
但是,我发现如果我将通知 class 中的 toArray()
函数更改为此,它会打印出很好的结果:
public function toArray()
{
$this->message->topic;
Log::info($this->message);
return [
'message_id' => $this->message->id,
];
}
为什么?我该如何解决这个问题?
注意:此 question/answer 仅与 Laravel < 5.6 有关。从Laravel 5.6开始,加载的关系也被序列化了,所以本题的问题不再是问题
您的通知设置为排队,并且您要扩展的 Notification
class 使用 SerializesModels
特性。当具有 SerializesModels
特征的对象被序列化以放入队列时,该对象中包含的任何模型(例如您的消息)将仅替换为该模型的 ID(消息 ID)。当队列工作者反序列化您的通知以处理它时,它将使用该消息 ID 从数据库中重新检索消息。不幸的是,发生这种情况时,不包括任何关系。
因此,即使您的消息在序列化时加载了主题关系,在队列工作者处理通知时也不会加载主题关系。如果您需要通知中的主题,您将需要重新加载它,如您所见。
您可以在 documentation here 中阅读更多相关信息。相关部分引述如下:
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.
我创建了一个通知,我正在将模型传递给:
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,
];
}
}
这就是我调用通知的方式:
$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
关系没有显示。
但是,我发现如果我将通知 class 中的 toArray()
函数更改为此,它会打印出很好的结果:
public function toArray()
{
$this->message->topic;
Log::info($this->message);
return [
'message_id' => $this->message->id,
];
}
为什么?我该如何解决这个问题?
注意:此 question/answer 仅与 Laravel < 5.6 有关。从Laravel 5.6开始,加载的关系也被序列化了,所以本题的问题不再是问题
您的通知设置为排队,并且您要扩展的 Notification
class 使用 SerializesModels
特性。当具有 SerializesModels
特征的对象被序列化以放入队列时,该对象中包含的任何模型(例如您的消息)将仅替换为该模型的 ID(消息 ID)。当队列工作者反序列化您的通知以处理它时,它将使用该消息 ID 从数据库中重新检索消息。不幸的是,发生这种情况时,不包括任何关系。
因此,即使您的消息在序列化时加载了主题关系,在队列工作者处理通知时也不会加载主题关系。如果您需要通知中的主题,您将需要重新加载它,如您所见。
您可以在 documentation here 中阅读更多相关信息。相关部分引述如下:
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.