我正在用laravel创建学生管理系统的api。当教师在数据库中输入他们的记录说他们的录取成功时,我想向学生发送电子邮件。我怎样才能做到这一点?
答案 0 :(得分:0)
通知!
在插入记录的方法中,您要通知。 最终结果将如下所示:
use App\Notifications\StudentAdmitted;
...
public function doSomething(Request $request, Student $student)
{
$student->admitted = true;
$student->notify(new StudentAdmitted);
}
创建通知
php artisan make:notification StudentAdmitted
编辑
您要修改toMail方法
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Congratulations!')
->line('you have been accepted')
->action('View', url('/url/to/page'))
->line('something here');
}
此外,“学生”或“用户”模型应使用“可通知”特征。
...
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
....