更新于2017年1月14日:实现了更正确的方法
快速示例:
use Illuminate\Support\Facades\Notification;
use App\Notifications\SomethingCoolHappen;
Route::get('/step1', function () {
$followers = App\User::all();
Notification::send($followers, new SomethingCoolHappen(['arg1' => 1, 'arg2' => 2]));
});
Route::get('/step2', function () {
$user = App\User::find(10);
foreach ($user->unreadSubnotifications as $subnotification) {
var_dump($subnotification->notification->data);
$subnotification->markAsRead();
}
});
如何让它工作?
步骤1 - 迁移 - 创建表(subnotifications)
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSubnotificationsTable extends Migration
{
public function up()
{
Schema::create('subnotifications', function (Blueprint $table) {
$table->increments('id')->primary();
$table->uuid('notification_id');
$table->morphs('notifiable');
$table->timestamp('read_at')->nullable();
});
}
public function down()
{
Schema::dropIfExists('subnotifications');
}
}
第二步 - 让我们为新的子通知表创建一个模型
<?php
namespace App\Notifications;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\DatabaseNotificationCollection;
class Subnotification extends Model
{
public $timestamps = false;
protected $guarded = [];
protected $casts = [
'read_at' => 'datetime',
];
public function notification()
{
return $this->belongsTo(DatabaseNotification::class);
}
public function notifiable()
{
return $this->morphTo();
}
public function markAsRead()
{
if (is_null($this->read_at)) {
$this->forceFill(['read_at' => $this->freshTimestamp()])->save();
}
}
}
第三步 - 创建自定义数据库通知渠道
更新:使用静态变量$map来保留第一个通知的id,并插入下一个通知(具有相同的数据),而无需在notifications
表中创建记录。
<?php
namespace App\Channels;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\Notification;
class SubnotificationsChannel
{
public function send($notifiable, Notification $notification)
{
static $map = [];
$notificationId = $notification->id;
$data = $this->getData($notifiable, $notification);
$hash = md5(json_encode($data));
if (!isset($map[$hash])) {
DatabaseNotification::create([
'id' => $notificationId,
'type' => get_class($notification),
'notifiable_id' => 0,
'notifiable_type' => get_class($notifiable),
'data' => $data,
'read_at' => null,
]);
$map[$hash] = $notificationId;
} else {
$notificationId = $map[$hash];
}
$notifiable->subnotifications()->create([
'notification_id' => $notificationId,
'read_at' => null
]);
}
public function getData($notifiable, Notification $notification)
{
return $notification->toArray($notifiable);
}
}
第四步 - 创建通知
更新: 现在通知支持所有渠道,不仅仅是子通知。
<?php
namespace App\Notifications;
use App\Channels\SubnotificationsChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class SomethingCoolHappen extends Notification
{
use Queueable;
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function via($notifiable)
{
$via = [];
$via[] = SubnotificationsChannel::class;
return $via;
}
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}
public function toArray($notifiable)
{
return $this->data;
}
}
步骤五 - “followers” 助手特性
<?php
namespace App\Notifications;
trait HasSubnotifications
{
public function Subnotifications()
{
return $this->morphMany(Subnotification::class, 'notifiable')
->orderBy('id', 'desc');
}
public function readSubnotifications()
{
return $this->Subnotifications()
->whereNotNull('read_at');
}
public function unreadSubnotifications()
{
return $this->Subnotifications()
->whereNull('read_at');
}
}
Step 6 - update your Users model
Updated: no required followers method
namespace App;
use App\Notifications\HasSubnotifications;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
use HasSubnotifications;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
toMail
丢失了,我有一些逻辑来查找想要电子邮件的关注者,但是我应该把那个逻辑放在哪里呢?由于示例使用$user->notify(new FollowersUpdate(...
,当我在第4步中放置toMail
时,它会向$user
发送电子邮件,这不是我们想要的,因为我们想要使用一些用户详细信息向关注者发送电子邮件。我认为将其放入FollowerChannel
中并不正确,因为它处理数据库表条目?您有什么想法可以实现正确的位置吗? - WonkaNotifications
:原始通知与短信/电子邮件以及FollowersUpdate
(以进行子通知)。我相信这对于您的优化来说是微不足道的代价。 - Leonid Shumakov$follower
进行foreach以确定他们的偏好并循环通知,而不是发送$followers
并在通道中处理偏好。 - Wonka$notifiable
是您关注者的 Eloquent 模型。仅向 Gmail 用户发送电子邮件:if (stripos($notifiable->email, '@gmail.com') !== false) $via[] = 'mail';
- Leonid Shumakov