kla*_*aaz 3 notifications database-connection laravel-5.3
我目前正在实现Laravel 5.3 Notifications,它工作得非常好。
目前,我正在使用“电子邮件”作为通知渠道,但我也想添加“数据库”。我为语言使用了不同的数据库/连接,并希望将通知存储在中央数据库/连接中。
如何将其他数据库连接用于通知?
我已经尝试创建一个通知模型,但是没有用:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notifications extends Model
{
protected $connection = 'system';
}
Run Code Online (Sandbox Code Playgroud)
小智 5
棘手的解决方案。但是在MongoDB连接上进行了尝试和测试。
需要修改的内容;
Notifiable特点DatabaseNotification模型HasNotifications特征DatabaseNotificationCollection再次修改.A对于非MySQL连接非常有用Notifiable特征复制内容Illuminate\Notifications\Notifiable并在您的自定义路径中创建一个新文件...说App\Overrides\Notifications\Notifiable。
您的文件将进行两项更改...命名空间,并且RoutesNotifications由于我们没有将其复制过来,因此您必须加载特征。
<?php
namespace App\Overrides\Notifications;
use use Illuminate\Notifications\RoutesNotifications;
trait Notifiable{
//The rest of the code remains
}
Run Code Online (Sandbox Code Playgroud)
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';
}
Run Code Online (Sandbox Code Playgroud)
至此,如果您使用的是mysql连接,则应该可以使用。
要尝试此操作,请更改Notifiable要使用的用户模型的特征App\Overrides\Notifications\Notifiable。通知将使用您指定的连接。
MongoDB中的用户将不得不采取额外的步骤,因为最流行的司机,我知道的还不支持MorphMany关系被投入使用的Laravel通知。
既然不是问的问题,我们就这样:-)