Laravel更改通知的连接表 [英] Laravel change Connection for Notifications table

查看:136
本文介绍了Laravel更改通知的连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在实施Laravel 5.3通知,效果非常好.

I am implementing Laravel 5.3 Notifications at the moment which is working very nice.

目前,我正在使用电子邮件"作为通知渠道,但我也想添加数据库".我将不同的数据库/连接用于语言,并希望将通知存储在中央数据库/连接中.

At the moment I am using 'email' as a notifications channel but I want to add 'database' too. I am using different databases/connections for languages and want to store the notifications in a central database / Connection.

如何将其他数据库连接用于通知?

我已经尝试创建Notifications模型,但是没有用:

I already tried creating a Notifications model but that did not work:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Notifications extends Model
{
    protected $connection = 'system';
}

推荐答案

拙劣的解决方案.但是在MongoDB连接上进行了尝试和测试.

Hackish solution. But tried and tested on a MongoDB connection.

需要修改的内容;

  1. Notifiable特征
  2. DatabaseNotification模型
  3. 可选地(如果使用的是mysql,则保持不变)修改HasNotifications特质
  4. 修改DatabaseNotificationCollection.同样,这对于非mysql连接很有用
  1. The Notifiable trait
  2. The DatabaseNotification model
  3. Optionally (nothing changes if you are using mysql) modify the HasNotifications trait
  4. Modify the DatabaseNotificationCollection.Again this is useful for a non-mysql connection

第一步:创建自定义Notifiable特质

Illuminate\Notifications\Notifiable复制内容并在您的自定义路径中创建一个新文件...说App\Overrides\Notifications\Notifiable.

Step One : Create a custom Notifiable Trait

Copy the contents from Illuminate\Notifications\Notifiable and create a new file in your custom path...say App\Overrides\Notifications\Notifiable.

您的文件将进行两项更改...命名空间,由于我们没有将其复制,因此您必须加载RoutesNotifications特征.

Your file will feature two changes...the namespace and you have to load the RoutesNotifications trait since we are not copying it over.

<?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

Step Two : Create a custom DatabaseNotification model

Follow the same procedure as above and copy the contents of the Illuminate\Notifications\DatabaseNotification file to the custom path that we created above...App\Overrides\Notification\DatabaseNotification

这是一个标准的口才模型,连接更改实际上在此处发生

This is a standard Eloquent model and the connection change actually happens here

<?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连接,这应该可以正常工作.

As of this point this should work if you are on a mysql connection.

要尝试此操作,请将用户模型上的Notifiable特征更改为使用App\Overrides\Notifications\Notifiable.通知将使用您指定的连接.

To try this out change the Notifiable trait on the user model to use App\Overrides\Notifications\Notifiable. The notifications will use the connection you specified.

由于最受欢迎的驱动程序,MongoDB的用户将不得不采取额外的步骤尚未支持MorphMany关系,该关系已用于Laravel通知.

Users of MongoDB will have to take extra steps since the most popular driver I know of does not yet support MorphMany relations which are put to use for Laravel notifications.

因为这不是问的问题,所以我们将其留在:-)

Since that is not the asked question we leave it at that :-)

这篇关于Laravel更改通知的连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆