RelationNotFoundException 错误仅在 laravel 个派遣作业中
RelationNotFoundException error only in laravel dispatched jobs
我在 laravel 7 中有一个非常奇怪的错误,我在用户模型中定义了一个名为 user_badge
的 hasOne 关系
public function userBadge()
{
return $this->hasOne(UserBadge::class, 'id', 'user_badge_id');
}
我已将 user_badge_id 添加到用户 table 并且我还创建了包含徽章信息的 user_badges table。当作业在后台运行时,出现以下错误
Illuminate\Database\Eloquent\RelationNotFoundException:调用模型 [App\User] 上的未定义关系 [userBadge]。在 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/RelationNotFoundException.php:34
在整个站点中导航完全没有问题。
向作业队列发送电子邮件时会出现此问题。它发生在所有发送的通知上。除此之外,有时确实会发送电子邮件,但大多数时候不会。下面是通知之一的代码
class NewFollowUserNotification extends Notification implements ShouldQueue
{
use Queueable;
public $sender;
public $receiver;
/**
* Create a new notification instance.
*
* @param User $sender
* @param User $receiver
*/
public function __construct(User $sender, User $receiver)
{
$this->sender = $sender;
$this->receiver = $receiver;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
if (Profile::sendEmailNotification('NewFollowUserNotification', $this->receiver)) {
return ['database', 'mail', 'broadcast'];
} else {
return ['database', 'broadcast'];
}
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$user_notifications = UserNotifications::create('NewFollowUserNotification');
$user_notifications->setUsername($this->receiver->username);
$user_notifications->setEmailTemplate(EmailTemplate::getDefaultEmail());
$user_notifications->setButtonUrl(config('app.url').'/member/profiles/'.$this->sender->username);
$notification = $user_notifications->getEmailNotification();
$user_notifications->setTags('{receiver_first_name}', $this->receiver->first_name);
$user_notifications->setTags('{receiver_last_name}', $this->receiver->last_name);
$user_notifications->setTags('{receiver_full_name}', $this->receiver->first_name . ' ' . $this->receiver->last_name);
$user_notifications->setTags('{sender_first_name}', $this->sender->first_name);
$user_notifications->setTags('{sender_last_name}', $this->sender->last_name);
$user_notifications->setTags('{sender_full_name}', $this->sender->first_name . ' ' . $this->sender->last_name);
return $user_notifications->sendUserNotification($notification);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'heading' => 'New contact request',
'message' => $this->sender->first_name.' '.$this->sender->last_name.
' sent you a contact request',
'link' => '/member/profiles/'.$this->sender->username,
'username' => $this->sender->username,
'module' => 'user',
];
}
在我自己的开发机器上,一切正常。
这里的问题是作业中模型的序列化。作业可以序列化多少数据是有限制的,所以我最终将模型的 ID 传递给作业并从那里访问必要的信息。
我在 laravel 7 中有一个非常奇怪的错误,我在用户模型中定义了一个名为 user_badge
的 hasOne 关系public function userBadge()
{
return $this->hasOne(UserBadge::class, 'id', 'user_badge_id');
}
我已将 user_badge_id 添加到用户 table 并且我还创建了包含徽章信息的 user_badges table。当作业在后台运行时,出现以下错误
Illuminate\Database\Eloquent\RelationNotFoundException:调用模型 [App\User] 上的未定义关系 [userBadge]。在 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/RelationNotFoundException.php:34
在整个站点中导航完全没有问题。
向作业队列发送电子邮件时会出现此问题。它发生在所有发送的通知上。除此之外,有时确实会发送电子邮件,但大多数时候不会。下面是通知之一的代码
class NewFollowUserNotification extends Notification implements ShouldQueue
{
use Queueable;
public $sender;
public $receiver;
/**
* Create a new notification instance.
*
* @param User $sender
* @param User $receiver
*/
public function __construct(User $sender, User $receiver)
{
$this->sender = $sender;
$this->receiver = $receiver;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
if (Profile::sendEmailNotification('NewFollowUserNotification', $this->receiver)) {
return ['database', 'mail', 'broadcast'];
} else {
return ['database', 'broadcast'];
}
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$user_notifications = UserNotifications::create('NewFollowUserNotification');
$user_notifications->setUsername($this->receiver->username);
$user_notifications->setEmailTemplate(EmailTemplate::getDefaultEmail());
$user_notifications->setButtonUrl(config('app.url').'/member/profiles/'.$this->sender->username);
$notification = $user_notifications->getEmailNotification();
$user_notifications->setTags('{receiver_first_name}', $this->receiver->first_name);
$user_notifications->setTags('{receiver_last_name}', $this->receiver->last_name);
$user_notifications->setTags('{receiver_full_name}', $this->receiver->first_name . ' ' . $this->receiver->last_name);
$user_notifications->setTags('{sender_first_name}', $this->sender->first_name);
$user_notifications->setTags('{sender_last_name}', $this->sender->last_name);
$user_notifications->setTags('{sender_full_name}', $this->sender->first_name . ' ' . $this->sender->last_name);
return $user_notifications->sendUserNotification($notification);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'heading' => 'New contact request',
'message' => $this->sender->first_name.' '.$this->sender->last_name.
' sent you a contact request',
'link' => '/member/profiles/'.$this->sender->username,
'username' => $this->sender->username,
'module' => 'user',
];
}
在我自己的开发机器上,一切正常。
这里的问题是作业中模型的序列化。作业可以序列化多少数据是有限制的,所以我最终将模型的 ID 传递给作业并从那里访问必要的信息。