更新 2017-01-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 - 迁移 - 创建表(子通知)
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');
}
}
步骤 2 - 让我们为新的子注释表创建一个模型
<?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();
}
}
}
步骤 3 - 创建自定义数据库通知通道
更新:使用静态变量 $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);
}
}
步骤 4 - 创建通知
更新:现在通知支持所有通道,而不仅仅是子通知
<?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;
}
}
第5步 - “追随者”的帮助者特征
<?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');
}
}
步骤6 - 更新您的用户模型
更新:不需要关注者方法
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',
];
}