0

我在 laravel 7 中有一个非常奇怪的错误,我在用户模型中定义了一个名为 user_badge 的 hasOne 关系

public function userBadge()
{
    return $this->hasOne(UserBadge::class, 'id', 'user_badge_id');
}

我已将 user_badge_id 添加到 users 表中,并且还创建了包含徽章信息的 user_badges 表。当作业在后台运行时,我收到以下错误

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',
        ];
    }

在我自己的开发机器上本地一切正常。

4

1 回答 1

0

这里的问题是作业中模型的序列化。关于可以序列化多少数据作业存在限制,因此我最终将模型的 id 传递给作业并从那里访问必要的信息。

于 2021-11-25T22:30:40.940 回答