如何修复在 Laravel 中找不到的类“App\Http\Controllers\Notification”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41812005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:11:40  来源:igfitidea点击:

How to fix Class 'App\Http\Controllers\Notification' not found in laravel?

phplaravellaravel-5facade

提问by Jaqueline Passos

I have a Controller which listens to a new Schedule creation and sends the result back to the view via ajax. Inside of it I want to add a Notification to send email to the user once the Schedule cannot be completed due to a lack of resources at that specific date and time.

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

The problem is that I get the error below:

问题是我收到以下错误:

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

The folder structure is this:

文件夹结构是这样的:

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

DadosAgendamentoController.phphead code:

DadosAgendamentoController.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;

line 88 and 89:

第 88 和 89 行:

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

Trough my Controller I can access all the classes above, but not the AgendamentoPendente

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

My goal is to send an email do the admin so he can suggest a new date and time to the client when the resources are not available at the desired date and time.

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

How can it be fixed? Can I access the class in this Controller? How?

如何修复?我可以访问这个控制器中的类吗?如何?

回答by Alexey Mezenin

Notifications may be sent in two ways: using the notify method of the Notifiable trait or using the Notification facade.

通知可以通过两种方式发送:使用 Notifiable trait 的 notify 方法或使用 Notification facade。

https://laravel.com/docs/5.3/notifications#sending-notifications

https://laravel.com/docs/5.3/notifications#sending-notifications

Option 1

选项1

You can use notify()method:

您可以使用notify()方法:

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

Also, make sure Userclass uses Notifiabletrait:

另外,请确保User类使用Notifiabletrait:

use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

Option 2

选项 2

Using facade with full namespace:

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

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

回答by Paras

Add use Notification;in your controller

添加use Notification;您的控制器

OR

或者

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

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

回答by tahrin_aziz

add this at the top of the controller:

在控制器顶部添加:

use App\Notifications\AgendamentoPendente;

i was having the same problem and this fixed it

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

回答by Kwaye Kant

Also note that if you are using the facade, make sure your User queries the email field from your database

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

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