Laravel: 自定义或扩展通知 - 数据库模型
Laravel: customize or extend notifications - database model
恕我直言,Laravel 中用于保存通知的当前数据库通道设计非常糟糕:
- 例如,您不能在项目上使用外键级联来清理已删除项目的通知
- 在
data
列(转换为数组)中搜索自定义属性不是最优的
您将如何扩展供应商包中的 DatabaseNotification
模型?
我想将列 event_id
、question_id
、user_id
(创建通知的用户)等添加到默认 laravel notifications
table
如何覆盖 send
函数以包含更多列?
在:
vendor/laravel/framework/src/Illuminate/Notifications/Channels/DatabaseChannel.php
代码:
class DatabaseChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Illuminate\Database\Eloquent\Model
*/
public function send($notifiable, Notification $notification)
{
return $notifiable->routeNotificationFor('database')->create([
'id' => $notification->id,
'type' => get_class($notification),
\I want to add these
'user_id' => \Auth::user()->id,
'event_id' => $notification->type =='event' ? $notification->id : null,
'question_id' => $notification->type =='question' ? $notification->id : null,
\End adding new columns
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
]);
}
}
创建自定义通知渠道:
首先,在App\Notifications中创建一个Class,例如:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
class CustomDbChannel
{
public function send($notifiable, Notification $notification)
{
$data = $notification->toDatabase($notifiable);
return $notifiable->routeNotificationFor('database')->create([
'id' => $notification->id,
//customize here
'answer_id' => $data['answer_id'], //<-- comes from toDatabase() Method below
'user_id'=> \Auth::user()->id,
'type' => get_class($notification),
'data' => $data,
'read_at' => null,
]);
}
}
其次,在Notification中的via
方法中使用这个通道class:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use App\Notifications\CustomDbChannel;
class NewAnswerPosted extends Notification
{
private $answer;
public function __construct($answer)
{
$this->answer = $answer;
}
public function via($notifiable)
{
return [CustomDbChannel::class]; //<-- important custom Channel defined here
}
public function toDatabase($notifiable)
{
return [
'type' => 'some data',
'title' => 'other data',
'url' => 'other data',
'answer_id' => $this->answer->id //<-- send the id here
];
}
}
我通过自定义通知解决了类似的问题class:
为此操作创建 class:
artisan make:notification NewQuestion
里面:
public function __construct($user,$question)
{
$this->user=$user;
$this->question=$question;
}
...
public function toDatabase($notifiable){
$data=[
'question'=>$this->(array)$this->question->getAttributes(),
'user'=>$this->(array)$this->user->getAttributes()
];
return $data;
}
然后您可以像这样在视图或控制器中访问适当的数据:
@if($notification->type=='App\Notifications\UserRegistered')
<a href="{!!route('question.show',$notification->data['question']['id'])!!}">New question from {{$notification->data['user']['name']}}</a>
@endif
创建并使用您自己的 Notification
模型和 Notifiable
特征,然后在您的(用户)模型中使用您自己的 Notifiable 特征。
App\Notifiable.php:
namespace App;
use Illuminate\Notifications\Notifiable as BaseNotifiable;
trait Notifiable
{
use BaseNotifiable;
/**
* Get the entity's notifications.
*/
public function notifications()
{
return $this->morphMany(Notification::class, 'notifiable')
->orderBy('created_at', 'desc');
}
}
App\Notification.php:
namespace App;
use Illuminate\Notifications\DatabaseNotification;
class Notification extends DatabaseNotification
{
// ...
}
App\User.php:
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
// ...
}
与 "Bassem El Hachem" 不同,我想在 via()
方法中保留 database
关键字。
所以除了自定义 DatabaseChannel
之外,我还在 createDatabaseDriver()
方法中编写了自己的 ChannelManager
,return 是我自己的 DatabaseChannel
。
在我的应用程序的 ServiceProvider::register()
方法中,我将原始 ChannelManager class 的单例覆盖为 return 我的自定义管理器。
的示例。
如果您确实需要扩展 Illuminate\Notifications\Channels\DatabaseChannel
而不是创建新频道,您可以:
扩展频道:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Channels\DatabaseChannel as BaseDatabaseChannel;
use Illuminate\Notifications\Notification;
class MyDatabaseChannel extends BaseDatabaseChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Illuminate\Database\Eloquent\Model
*/
public function send($notifiable, Notification $notification)
{
$adminNotificationId = null;
if (method_exists($notification, 'getAdminNotificationId')) {
$adminNotificationId = $notification->getAdminNotificationId();
}
return $notifiable->routeNotificationFor('database')->create([
'id' => $notification->id,
'type' => get_class($notification),
'data' => $this->getData($notifiable, $notification),
// ** New custom field **
'admin_notification_id' => $adminNotificationId,
'read_at' => null,
]);
}
}
并再次在应用程序容器上注册 Illuminate\Notifications\Channels\DatabaseChannel
:
app\Providers\AppServiceProvider.php
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
Illuminate\Notifications\Channels\DatabaseChannel::class,
App\Notifications\MyDatabaseChannel::class
);
}
}
现在当 Illuminate\Notifications\ChannelManager
尝试 createDatabaseDriver
时将 return 您注册的数据库驱动程序。
多一个选项解决这个问题!
恕我直言,Laravel 中用于保存通知的当前数据库通道设计非常糟糕:
- 例如,您不能在项目上使用外键级联来清理已删除项目的通知
- 在
data
列(转换为数组)中搜索自定义属性不是最优的
您将如何扩展供应商包中的 DatabaseNotification
模型?
我想将列 event_id
、question_id
、user_id
(创建通知的用户)等添加到默认 laravel notifications
table
如何覆盖 send
函数以包含更多列?
在:
vendor/laravel/framework/src/Illuminate/Notifications/Channels/DatabaseChannel.php
代码:
class DatabaseChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Illuminate\Database\Eloquent\Model
*/
public function send($notifiable, Notification $notification)
{
return $notifiable->routeNotificationFor('database')->create([
'id' => $notification->id,
'type' => get_class($notification),
\I want to add these
'user_id' => \Auth::user()->id,
'event_id' => $notification->type =='event' ? $notification->id : null,
'question_id' => $notification->type =='question' ? $notification->id : null,
\End adding new columns
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
]);
}
}
创建自定义通知渠道:
首先,在App\Notifications中创建一个Class,例如:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
class CustomDbChannel
{
public function send($notifiable, Notification $notification)
{
$data = $notification->toDatabase($notifiable);
return $notifiable->routeNotificationFor('database')->create([
'id' => $notification->id,
//customize here
'answer_id' => $data['answer_id'], //<-- comes from toDatabase() Method below
'user_id'=> \Auth::user()->id,
'type' => get_class($notification),
'data' => $data,
'read_at' => null,
]);
}
}
其次,在Notification中的via
方法中使用这个通道class:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use App\Notifications\CustomDbChannel;
class NewAnswerPosted extends Notification
{
private $answer;
public function __construct($answer)
{
$this->answer = $answer;
}
public function via($notifiable)
{
return [CustomDbChannel::class]; //<-- important custom Channel defined here
}
public function toDatabase($notifiable)
{
return [
'type' => 'some data',
'title' => 'other data',
'url' => 'other data',
'answer_id' => $this->answer->id //<-- send the id here
];
}
}
我通过自定义通知解决了类似的问题class:
为此操作创建 class:
artisan make:notification NewQuestion
里面:
public function __construct($user,$question)
{
$this->user=$user;
$this->question=$question;
}
...
public function toDatabase($notifiable){
$data=[
'question'=>$this->(array)$this->question->getAttributes(),
'user'=>$this->(array)$this->user->getAttributes()
];
return $data;
}
然后您可以像这样在视图或控制器中访问适当的数据:
@if($notification->type=='App\Notifications\UserRegistered')
<a href="{!!route('question.show',$notification->data['question']['id'])!!}">New question from {{$notification->data['user']['name']}}</a>
@endif
创建并使用您自己的 Notification
模型和 Notifiable
特征,然后在您的(用户)模型中使用您自己的 Notifiable 特征。
App\Notifiable.php:
namespace App;
use Illuminate\Notifications\Notifiable as BaseNotifiable;
trait Notifiable
{
use BaseNotifiable;
/**
* Get the entity's notifications.
*/
public function notifications()
{
return $this->morphMany(Notification::class, 'notifiable')
->orderBy('created_at', 'desc');
}
}
App\Notification.php:
namespace App;
use Illuminate\Notifications\DatabaseNotification;
class Notification extends DatabaseNotification
{
// ...
}
App\User.php:
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
// ...
}
与 "Bassem El Hachem" 不同,我想在 via()
方法中保留 database
关键字。
所以除了自定义 DatabaseChannel
之外,我还在 createDatabaseDriver()
方法中编写了自己的 ChannelManager
,return 是我自己的 DatabaseChannel
。
在我的应用程序的 ServiceProvider::register()
方法中,我将原始 ChannelManager class 的单例覆盖为 return 我的自定义管理器。
如果您确实需要扩展 Illuminate\Notifications\Channels\DatabaseChannel
而不是创建新频道,您可以:
扩展频道:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Channels\DatabaseChannel as BaseDatabaseChannel;
use Illuminate\Notifications\Notification;
class MyDatabaseChannel extends BaseDatabaseChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Illuminate\Database\Eloquent\Model
*/
public function send($notifiable, Notification $notification)
{
$adminNotificationId = null;
if (method_exists($notification, 'getAdminNotificationId')) {
$adminNotificationId = $notification->getAdminNotificationId();
}
return $notifiable->routeNotificationFor('database')->create([
'id' => $notification->id,
'type' => get_class($notification),
'data' => $this->getData($notifiable, $notification),
// ** New custom field **
'admin_notification_id' => $adminNotificationId,
'read_at' => null,
]);
}
}
并再次在应用程序容器上注册 Illuminate\Notifications\Channels\DatabaseChannel
:
app\Providers\AppServiceProvider.php
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
Illuminate\Notifications\Channels\DatabaseChannel::class,
App\Notifications\MyDatabaseChannel::class
);
}
}
现在当 Illuminate\Notifications\ChannelManager
尝试 createDatabaseDriver
时将 return 您注册的数据库驱动程序。
多一个选项解决这个问题!