【问题标题】:Laravel Notifications Base Table not found未找到 Laravel 通知基表
【发布时间】:2018-05-22 21:15:14
【问题描述】:

我正在做一个 Laravel 项目。我运行了这些命令并成功创建了通知表。

php artisan notifications:table
php artisan migrate

所有的事情都进行得很顺利。后来我创建了一个名为“NotificationsForAdmin”的模型名称,迁移名为“notifications_for_admin”,后来我放弃了这个表。现在,当我尝试生成一些通知时,它向我显示了这个错误,我不知道发生了什么我在数据库中有通知表,这是具有完美模式的 laravel 通知所需的。错误是:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'followup.notification_for_admins' doesn't exist (SQL: select * from `notification_for_admins` where `notification_for_admins`.`user_id` = 2 and `notification_for_admins`.`user_id` is not null)

我的通知是:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use App\User;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
use App\Events\NewEmailReceivedEvent;
use Auth;

    class NewEmailReceived extends Notification
    {
        use Queueable;

        public $sender_id, $receiver_id, $sender_name, $receiver_name, $sender_type, $receiver_type, $type, $recipient, $from_email, $subject, $message, $image, $receiver_image, $attachments, $sizesOfAttachments, $originalFileNames, $thread_id, $id_of_email;

        /**
         * Create a new notification instance.
         *
         * @return void
         */
        public function __construct($sender_id, $receiver_id, $sender_name, $receiver_name, $sender_type, $receiver_type, $type, $recipient, $from_email, $subject, $message, $image, $receiver_image, $attachments, $sizesOfAttachments, $originalFileNames, $thread_id, $id_of_email)
        {
            $this->sender_id = $sender_id;
            $this->receiver_id = $receiver_id;
            $this->sender_name = $sender_name;
            $this->receiver_name = $receiver_name;
            $this->sender_type = $sender_type;
            $this->receiver_type = $receiver_type;
            $this->type = $type;
            $this->recipient = $recipient;
            $this->from_email = $from_email;
            $this->subject = $subject;
            $this->message = $message;
            $this->image = $image;
            $this->receiver_image = $receiver_image;
            $this->attachments = $attachments;
            $this->sizesOfAttachments = $sizesOfAttachments;
            $this->originalFileNames = $originalFileNames;
            $this->thread_id = $thread_id;
            $this->id_of_email = $id_of_email;
        }

        /**
         * Get the notification's delivery channels.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function via($notifiable)
        {
            $notifications = Auth::user()->notifications;
            if ($notifications[7]->shown == 1) {
                return ['mail', 'database'];
            }
            else{
                return ['database'];
            }
        }

        /**
         * Get the array representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function toDatabase($notifiable)
        {
            return [
                'sender_id' => $this->sender_id,
                'receiver_id' => $this->receiver_id,
                'sender_name' => $this->sender_name,
                'receiver_name' => $this->receiver_name,
                'sender_type' => $this->sender_type,
                'receiver_type' => $this->receiver_type,
                'type' => $this->type,
                'recipient' => $this->recipient,
                'from_email' => $this->from_email,
                'subject' => $this->subject,
                'message' => $this->message,
                'image' => $this->image,
                'receiver_image' => $this->receiver_image,
                'attachments' => $this->attachments,
                'sizesOfAttachments' => $this->sizesOfAttachments,
                'originalFileNames' => $this->originalFileNames,
                'thread_id' => $this->thread_id,
                'id_of_email' => $this->id_of_email,
            ];
            event(new NewEmailReceivedEvent($NewEmailReceivedRequest));
            return $NewEmailReceivedRequest;
        }

        /**
         * Get the mail representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return \Illuminate\Notifications\Messages\MailMessage
         */
        public function toMail($notifiable)
        {
            return (new MailMessage)
                ->subject("New email from ".$this->sender_type)
                ->greeting('Hello!')
                ->markdown('mails.NewEmailReceived' , ['recipient_name' => $this->receiver_name , 'subject' => $this->subject , 'mailMessage' => str_limit($this->message, 50) , 'avatar' => $this->image]);
        }


        /**
         * Get the array representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function toArray($notifiable)
        {
            return [
                //
            ];
        }
    }

如果有人可以帮助我,我将非常感激。

【问题讨论】:

  • 你在数据库中的表名是什么?

标签: mysql laravel notifications laravel-5.5 pusher


【解决方案1】:

您的User 对象上的notifications 关系似乎仍然引用NotificationsForAdmin

如果您不为模型指定表,则表会自动生成为模型名称的蛇形大小写字符串。在NotificationsForAdmin 的情况下,这将变为notification_for_admins

将公共属性$table 添加到您的NotificationsForAdmin,并将存储通知的表的名称作为值。这应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 2013-10-16
    • 2023-03-08
    • 1970-01-01
    • 2016-09-23
    • 2016-01-25
    • 1970-01-01
    • 2015-07-21
    • 2021-05-30
    相关资源
    最近更新 更多