我在一个带有 laravel 框架的网站项目上工作,我希望在单击按钮时发送通知或者发送到用户的电子邮件
$invite = Invite::create([
'name' => $request->get('name'),
'email' => $request->get('email'),
'token' => str_random(60),
]);
$invite->notify(new UserInvite());
tnx 来帮助我
我在一个带有 laravel 框架的网站项目上工作,我希望在单击按钮时发送通知或者发送到用户的电子邮件
$invite = Invite::create([
'name' => $request->get('name'),
'email' => $request->get('email'),
'token' => str_random(60),
]);
$invite->notify(new UserInvite());
tnx 来帮助我
您使用的是邮件通知,这是答案,但您可以参考 laravel 文档的通知部分以获取更多信息:
https://laravel.com/docs/5.4/notifications
首先使用项目文件夹中的终端生成通知:
php artisan make:notification UserInvite
然后在生成的文件中指定您的驱动程序为'Mail'
. 默认情况下它是。laravel 也有一个很好的示例代码。最好将您的 $invite 注入到通知中,这样您就可以在那里使用它。这是一个快速示例代码。您可以在 App\Notifications 下找到生成的通知。
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Invite;
class UserInvite extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
public function via($notifiable)
{
return ['mail']; // Here you specify your driver, your case is Mail
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Your greeting comes here')
->line('The introduction to the notification.') //here is your lines in email
->action('Notification Action', url('/')) // here is your button
->line("You can use {$notifiable->token}"); // another line and you can add many lines
}
}
现在您可以拨打您的通知:
$invite->notify(new UserInvite());
由于您是在邀请时通知,因此您的通知是相同的邀请。因此,在您的通知中,您可以使用它$notification->token
来检索invite object
.
如果我能提供任何帮助,请告诉我。问候。