2017-01-23 72 views
0

我有一個控制器,監聽新的計劃創建,並通過ajax將結果發送回視圖。在它的內部,我想添加一個通知來發送電子郵件給用戶,因爲在特定的日期和時間缺乏資源,無法完成時間表。如何解決在laravel中找不到類'App Http Controllers Notification'?

的問題是,我得到的錯誤如下:

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

我的目標是發送電子郵件做管理員,所以他可以提出一個新的日期和時間的客戶端時資源在期望的日期和時間不可用。

如何解決?我可以訪問這個控制器中的類嗎?怎麼樣?

+0

請告訴我們'DadosAgendamentoController的第89行。php' –

+0

我幾乎可以肯定你沒有導入'Notification'類。只需導入命名空間,它應該被修復。如果通知是一個外觀,只需要調用'\ Notification :: foo()' – felipsmartins

+0

@AlexeyMezenin,我已經編輯了這個問題。 –

回答

4

可以通過兩種方式發送通知:使用Notifiable特徵的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)); 
+0

錯誤已解決,但未按預期工作......只有在數據庫上創建模型的新數據時,纔會發送Laravel通知?這是如何工作的?只有當表格字段值設置爲false時,我纔可以發送它嗎? –

2

添加use Notification;在控制器

OR

另外,使用\Notification::send($user, new AgendamentoPendente(1));

0

在控制器的頂部添加此:

use App\Notifications\AgendamentoPendente; 

我有同樣的問題,這個固定

相關問題