如何修复在laravel中找不到的类'App \ Http \ Controllers \ Notification'?

时间:2017-01-23 17:19:45

标签: php laravel laravel-5 facade

我有一个Controller,它监听新的Schedule创建,并通过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

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;

第88和89行:

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

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

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

如何修复?我可以在此Controller中访问该类吗?怎么样?

5 个答案:

答案 0 :(得分:6)

  

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

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

选项1

您可以使用notify()方法:

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

另外,请确保User班级使用Notifiable特征:

use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

选项2

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

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

答案 1 :(得分:4)

在控制器中添加use Notification;

OR

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

答案 2 :(得分:0)

在控制器顶部添加:

use App\Notifications\AgendamentoPendente;

我遇到了同样的问题并且修复了它

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

您必须在顶部使用立面

result = some_model.objects.all().filter(country__icontains=country)

您可以参考本教程

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