每次我尝试运行要发送通知的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存在。
答案 0 :(得分:1)
可以通过两种方式发送通知:使用“可通知”特征的notify方法或使用“通知外观”。 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));