关于是否使用Laravel的Notification或Mailable类,我有点困惑.根据我的理解,Mailables仅用于发送电子邮件,而通知可用于发送电子邮件和短信.在我的应用程序中,我暂时没有计划发送短信通知,所以如果我在这种情况下应该使用Mailable类,我很困惑.我的问题是:
如果我只是要发送电子邮件通知,我使用Mailables而不是Notifications更好吗?
如果每封电子邮件都有不同的html布局,那么Mailable会是更好的选择吗?
即使所有电子邮件都是通知电子邮件,使用Mailables而不是Notifications发送它们仍然有意义吗?
有人可以告诉我这两者之间的主要区别,以及我们应该如何决定在Laravel 5.3中发送电子邮件时选择哪种方法.
使用Laravel 5.3通知功能,我看到通知发送给用户,如下所示:
$user->notify(new InvoicePaid($invoice));
Run Code Online (Sandbox Code Playgroud)
我认为$user是值得注意的实体.如果我想向没有帐户的用户发送通知怎么办?在我的应用中,用户向他们的朋友发送邀请以加入.我正在尝试使用Laravel的通知将邀请代码发送给尚未拥有帐户的用户.
当用户邀请某人时,他们的数据存储在邀请模型中,如下所示:
class Invite extends Model
{
protected $table = 'invites';
protected $fillable = array('name', 'email', 'invite_code', 'expires_at', 'invited_by');
protected $dates = ['expires_at'];
}
Run Code Online (Sandbox Code Playgroud)
我以为我可以使用notify向Invite模型发送通知,如下所示:
$invite = Invite::find($inviteId);
$invite->notify(new InvitationNotification(ucfirst($invite->name), $invite->invite_code, $invite->expires_at));
Run Code Online (Sandbox Code Playgroud)
但上述方法无效.我收到错误:
Call to undefined method Illuminate\Database\Query\Builder::notify()
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
我能将通知仅发送给用户模型吗?
有没有办法将通知发送给没有帐户的新用户?
在这些情况下,我唯一的选择是使用Laravel的Mailable类而不是Notification吗?
我将通知保存到数据库中,如下所示:
public function toDatabase($notifiable)
{
return [
'from' => $this->message->name,
'name'=> $this->message->email,
'subject' => $this->message->subject,
'body' => $this->message->body
];
}
Run Code Online (Sandbox Code Playgroud)
它工作正常。现在我想将这些数据提取到我的视图中,所以我这样做:
@foreach ( Auth::user()->unreadNotifications as $notification)
<li><!-- start message -->
<a href="#">
<!-- Message title and timestamp -->
<h4>
{{ $notification->name }}
<small><i class="fa fa-clock-o"></i> 5 mins</small>
</h4>
<!-- The message -->
<p>{{ $notification->subject }}</p>
</a>
</li>
@endforeach
Run Code Online (Sandbox Code Playgroud)
但它什么也没给我。所以我做错了吗?
我使用数据库通知,在通知代码中我有方法toDatabase:
public function toDatabase($notifiable)
{
$user = \App\SomeUsers::where('id', $notifiable->id)->first();
return [
'message' => $message,
];
}
Run Code Online (Sandbox Code Playgroud)
它返回正在发送到via当前通知方法中提到的数据库通道的数据数组:
public function via($notifiable)
{
return ['database'];
}
Run Code Online (Sandbox Code Playgroud)
一切都像往常一样,但是......问题是我需要在当前通知文件中的数据库中的通知ID,以便我可以将消息(从当前通知文件)广播到包含数据库中通知ID的前端(所以我可以以某种方式将其标识为已读)。如何获得?
PS 此外,数据库通知可能是可排队的,所以...似乎我无法获得 id... PPS 另一个词我需要广播消息,其中包含["id" => "id of just added corresponding database notification"].
我想在通知电子邮件中添加用户的名字.目前,Laravel通知电子邮件开始如下:
Hello,
Run Code Online (Sandbox Code Playgroud)
我想将其更改为:
Hello Donald,
Run Code Online (Sandbox Code Playgroud)
现在,我有这样的设置.此示例适用于密码重置通知电子邮件:
用户模型:
public function sendPasswordResetNotification($token)
{
$this->notify(new PasswordReset($token));
}
Run Code Online (Sandbox Code Playgroud)
应用程序\声明\ PasswordReset:
class PasswordReset extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return …Run Code Online (Sandbox Code Playgroud) notifications laravel laravel-mail laravel-5.3 laravel-notification
我正在使用 Laravel 版本 5.7.20 构建自定义 Markdown 模板。/resources/views/vendor/notifications/email.blade.php
发出命令后生成的模板被复制php artisan vendor:publish --tag=laravel-notifications。
显示 HTML 的以下作品:
return (new MailMessage)
->line(new HtmlString('The <strong>introduction</strong> to the notification.'))
->line('The <strong>introduction</strong> to the notification.')
->line(new HtmlString('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i') . '</strong>'))
->line('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i') . '</strong>')
->action('Notification Action', url('/'));
Run Code Online (Sandbox Code Playgroud)

这是行不通的。这是使用我自己的降价
return (new MailMessage)
->line(new HtmlString('The <strong>introduction</strong> to the notification.'))
->line('The <strong>introduction</strong> to the notification.')
->line(new HtmlString('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i').'</strong>'))
->line('Due Date: <strong>' . Carbon::parse($this->info->created_at)->format('Y-m-d H:i').'</strong>')
->action('Notification …Run Code Online (Sandbox Code Playgroud) 我尝试在我的频道上发送一个简单的 slack 通知,以了解客户何时购买或注册,但我遇到了错误,我在网上找不到任何解决方案。
这是我的通知 SlackNotification.php :
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
class SlackNotification extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['slack'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed …Run Code Online (Sandbox Code Playgroud) Laravel 5.5
调节器
public function sendBookingSms(){
$checkState = session()->get('checkState');
$staffs = Staff::whereIn('staffId',$checkState)->get();
foreach ($staffs as $staff) {
$email = str_replace(" ","","44".substr($staff->mobile, 1)).'@mail.mightytext.net';
Notification::send($email, new NewBooking($email));
}
return $staffs;
session()->forget('checkState');
return redirect(route('booking.current'))->with('message','Succesfully Send SMS to selected staffs !!');
}
Run Code Online (Sandbox Code Playgroud)
NewBooking.php(通知)
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
Run Code Online (Sandbox Code Playgroud)
调用此控制器时,我收到此错误.
$员工.
{ "STAFFID是" 45 "的forName": "Eldhose", "姓": "约翰", "的categoryId":2, "电子邮件": "devhelloworld@gmail.com", "手机": "07588593278"," whatsappNumber ":" 57656578658" , "性别":1, …
laravel ×7
laravel-5.3 ×3
laravel-mail ×3
email ×2
laravel-5 ×2
php ×2
laravel-5.5 ×1
laravel-5.7 ×1
laravel-5.8 ×1
slack-api ×1