0

user_badgeというユーザーモデルでhasOne関係を定義したlaravel 7に非常に奇妙なエラーがあります

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 で

サイト全体をナビゲートしても、まったく問題はありません。

この問題は、電子メールをジョブ キューに送信するときに発生します。送信されるすべての通知に発生します。さらに、メールが送信されることもありますが、ほとんどの場合は送信されません。通知の1つのコードは以下のとおりです

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