我正在实现Laravel5.3通知,目前这是非常好的工作。
目前,我正在使用“电子邮件”作为通知渠道,但我也想添加“数据库”。我对语言使用不同的数据库/连接,并希望将通知存储在中央数据库/连接中。
如何使用不同的数据库连接进行通知?
我已经尝试过创建一个通知模型,但没有成功:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notifications extends Model
{
protected $connection = 'system';
}
发布于 2017-02-07 23:53:29
苦涩的溶液但在MongoDB连接上进行了尝试和测试。
哪些需要修改;
Notifiable
性状DatabaseNotification
模型HasNotifications
特性DatabaseNotificationCollection
.Again,这对于非mysql连接非常有用。第一步:创建自定义Notifiable
特性
从Illuminate\Notifications\Notifiable
复制内容并在自定义path...say App\Overrides\Notifications\Notifiable
中创建一个新文件。
您的文件将具有两个changes...the命名空间,您必须加载RoutesNotifications
特性,因为我们不会复制它。
<?php
namespace App\Overrides\Notifications;
use use Illuminate\Notifications\RoutesNotifications;
trait Notifiable{
//The rest of the code remains
}
第二步:创建自定义DatabaseNotification
模型
遵循上面相同的过程,并将Illuminate\Notifications\DatabaseNotification
文件的内容复制到我们在上面创建的自定义路径.App\Overrides\Notification\DatabaseNotification
这是一个标准的雄辩模型,连接变化实际上发生在这里。
<?php
namespace App\Overrides\Notification;
//Use this if on mongodb.otherwise use to Illuminate\Database\Eloquent\Model
use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Notifications\DatabaseNotificationCollection;
class DatabaseNotification extends Model
{
protected $connection = 'YOUR_CONNECTION_NAME_GOES HERE';
}
到目前为止,如果您是在mysql连接上,这应该可以工作。
要尝试这一点,请将用户模型上的Notifiable
特性更改为使用App\Overrides\Notifications\Notifiable
。通知将使用您指定的连接。
MongoDB的用户将不得不采取额外的步骤,因为我所知道的最受欢迎的司机还没有用于Laravel通知的关系。
既然这不是我们提出的问题,我们就把它留在这里:-)
发布于 2019-05-01 23:50:16
基于@Bernard答案的Laravel 5.7
User.php
<?php
namespace App;
// implement the override Notifiable trait
use App\Traits\Override\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
}
Notifiable.php
<?php
namespace App\Traits\Override;
use Illuminate\Notifications\RoutesNotifications;
trait Notifiable
{
use HasDatabaseNotifications, RoutesNotifications;
}
HasDatabaseNotifications.php
<?php
namespace App\Traits\Override;
use App\Models\Override\MultiConnectionDatabaseNotification;
trait HasDatabaseNotifications
{
/**
* Get the entity's notifications.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function notifications()
{
return $this->morphMany(MultiConnectionDatabaseNotification::class, 'notifiable')->orderBy('created_at', 'desc');
}
/**
* Get the entity's read notifications.
*
* @return \Illuminate\Database\Query\Builder
*/
public function readNotifications()
{
return $this->notifications()->whereNotNull('read_at');
}
/**
* Get the entity's unread notifications.
*
* @return \Illuminate\Database\Query\Builder
*/
public function unreadNotifications()
{
return $this->notifications()->whereNull('read_at');
}
}
MultiConnectionDatabaseNotification.php
<?php
namespace App\Models\Override;
use Illuminate\Notifications\DatabaseNotification as DatabaseNotification;
class MultiConnectionDatabaseNotification extends DatabaseNotification
{
// set your preferred connection here
protected $connection = 'oracle';
}
发布于 2020-07-15 00:25:04
这很简单,只需在Illuminate\Notifications\DatabaseNotification
添加Illuminate\Notifications\DatabaseNotification
仅此而已,它将起作用:)
如果要使用一个具有相同连接的通知表,则不需要创建新模型。
如果您对用户模型使用不同的连接,我的代码就会工作。
https://stackoverflow.com/questions/41521255
复制相似问题