【问题标题】:How to fix Class 'Illuminate\Notifications\Notification' not found?如何修复找不到类“Illuminate\Notifications\Notification”?
【发布时间】:2023-03-22 15:04:01
【问题描述】:

每次我尝试运行用于发送通知的 Test.php 文件时,都会收到错误消息: Fatal error: Class 'Illuminate\Notifications\Notification' not found in /Users/Macbook/app/app/Http/Controllers/Test.php on line 12.

这是我的 Test.php 文件:

<?php

namespace App\Notifications;

// use app\vendor\laravel\framework\src\Illuminate\Notifications\Notification;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\User;

class Test extends Notification
{
    use Queueable;

   /**
   * Create a new notification instance.
   *
   * @return void
   */

public $test;

public function __construct($test)
{
    $this->test = $test;
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['database'];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    /** 
    $url = url('/test/'.$this->test->id);

    return (new MailMessage)
                ->greeting('Hello!')
                ->line('The introduction to the notification.')
                ->action('Notification Action', $url)
                ->line('Thank you for using our application!');
    */
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toDatabase($notifiable)
{
    return [
        'data' => 'You have a new notification.',
        'from' => $this->message->name,
        'name'=> $this->message->email,
        'subject' => $this->message->subject,
        'body' => $this->message->body
    ];
}
}

我正在尝试使用数据库方法发送通知。谁能帮帮我?这很奇怪,因为我确信 Illuminate\Notifications\Notification 存在。

【问题讨论】:

    标签: php database laravel


    【解决方案1】:

    可以通过两种方式发送通知:使用 Notifiable trait 的 notify 方法或使用 Notification 门面。 https://laravel.com/docs/5.3/notifications#sending-notifications

    选项 1

    你可以使用notify()方法:

    $user->notify(new AgendamentoPendente(1));
    

    另外,确保 User 类使用 Notifiable trait:

    use Illuminate\Notifications\Notifiable;
    
    class User extends Authenticatable
    {
        use Notifiable;
    

    选项 2

    使用具有完整命名空间的外观:

    \Notification::send($user, new AgendamentoPendente(1));
    

    【讨论】:

    • 我应该把这段代码放在哪里?在 Test.php 里面?我仍然认为这不能解决缺少类的问题。
    • 嗨。只想问:如果使用选项1,第一行代码应该放在哪里?同样的问题也适用于选项 2。它显然不仅仅是一个命令行——它的 PHP 代码。我应该把它放在哪里?什么文件,在哪里?谢谢
    猜你喜欢
    • 2019-04-14
    • 2017-09-28
    • 2019-07-12
    • 2017-06-08
    • 2021-09-01
    • 2015-11-19
    • 2021-01-03
    • 2019-09-13
    • 1970-01-01
    相关资源
    最近更新 更多