【问题标题】:How to fix Class 'App\Http\Controllers\Notification' not found in laravel?如何修复 laravel 中未找到的“App\Http\Controllers\Notification”类?
【发布时间】:2017-06-08 07:07:13
【问题描述】:

我有一个控制器,它监听新的调度创建并通过 ajax 将结果发送回视图。在其中我想添加一个通知,以便在由于特定日期和时间缺乏资源而无法完成计划时向用户发送电子邮件。

问题是我收到以下错误:

Class 'App\Http\Controllers\Notification' not found in /laravel/app/Http/Controllers/DadosAgendamentoController.php on line 89

文件夹结构是这样的:

-app
    -Http
        -Controllers 
            DadosAgendamentoController.php
    -Notifications
        AgendamentoPendente.php

DadosAgenmentoController.php 头部代码:

namespace App\Http\Controllers;

use Input;
use Request;
use App\Servicos;
use App\Disponibilidades;
use App\Estabelecimentos;
use App\HorariosEstabelecimento;
use App\Agendamento;
use App\User;
use App\Notifications\AgendamentoPendente;

第 88 和 89 行:

$user = User::where('id',1)->get();
Notification::send($user, new AgendamentoPendente(1));

通过我的控制器,我可以访问上述所有类,但不能访问 AgenmentoPendente

我的目标是向管理员发送一封电子邮件,以便当资源在所需的日期和时间不可用时,他可以向客户建议一个新的日期和时间。

如何解决?我可以访问此控制器中的类吗?怎么样?

【问题讨论】:

  • 请告诉我们89th line of DadosAgendamentoController.php
  • 我几乎可以肯定你还没有导入 Notification 类。只需导入命名空间,它应该是固定的。如果 Notification 是一个门面,只需通过 \Notification::foo() 调用
  • @AlexeyMezenin,我已经编辑了问题。
  • @felipsmartins,我看到用户有一个使用通知的默认配置,我也尝试使用命令use Illuminate\Notifications\Notifiable;,但没有成功
  • @JaquelinePassos Alexey Mezenin 的回答是正确的。即使它不能解决您的问题,但在另一个地方/范围内存在问题。

标签: php laravel laravel-5 facade


【解决方案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));

【讨论】:

  • 错误已解决,但没有按预期工作... Laravel 通知仅在数据库上创建模型的新数据时发送?这是它的工作原理吗?只有当表字段值设置为 false 时,我才能发送它吗?
【解决方案2】:

在你的控制器中添加use Notification;

或者,使用\Notification::send($user, new AgendamentoPendente(1));

【讨论】:

    【解决方案3】:

    在控制器顶部添加:

    use App\Notifications\AgendamentoPendente;
    

    我遇到了同样的问题,这解决了它

    【讨论】:

      【解决方案4】:

      还要注意,如果您使用外观,请确保您的用户从您的数据库中查询电子邮件字段

      $users = User::select("email")->get();
      \Notification::send($users, new AgendamentoPendente(1));
      

      【讨论】:

        【解决方案5】:

        你必须在顶部使用外墙

        use Illuminate\Support\Facades\Notification;

        你可以参考这个教程

        https://thecodingsolution.com/view/laravel-notofication-laravel-database-notification

        【讨论】:

          【解决方案6】:

          你可以拉入使用 Lumen 8.0 的通知库:

          “照亮/通知”:“5.3.*” 进入您的composer.json,然后运行 ​​composer update 以拉入通知库。

          您还需要添加

          $app->register(Illuminate\Notifications NotificationServiceProvider::class);

          到你的 bootstrap/app.php

          这个过程对我有用。 谢谢

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-04-02
            • 2015-06-24
            • 2021-09-25
            • 2015-05-21
            • 2015-08-31
            • 2018-03-16
            • 2016-03-13
            • 1970-01-01
            相关资源
            最近更新 更多