Notifications 通知表的Laravel更改连接
目前我正在实现Laravel 5.3通知,它运行得非常好 目前,我正在使用“电子邮件”作为通知渠道,但我也想添加“数据库”。我正在为语言使用不同的数据库/连接,并希望将通知存储在中央数据库/连接中 如何使用不同的数据库连接进行通知? 我已经尝试创建通知模型,但没有成功:Notifications 通知表的Laravel更改连接,notifications,database-connection,laravel-5.3,Notifications,Database Connection,Laravel 5.3,目前我正在实现Laravel 5.3通知,它运行得非常好 目前,我正在使用“电子邮件”作为通知渠道,但我也想添加“数据库”。我正在为语言使用不同的数据库/连接,并希望将通知存储在中央数据库/连接中 如何使用不同的数据库连接进行通知? 我已经尝试创建通知模型,但没有成功: namespace App; use Illuminate\Database\Eloquent\Model; class Notifications extends Model { protected $connec
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notifications extends Model
{
protected $connection = 'system';
}
粗俗的解决方案。但在MongoDB连接上进行了尝试和测试 需要修改的内容
数据库通知
模型HasNotifications
traitDatabaseNotificationCollection
。这对于非mysql连接同样有用应呈报的特征
从illighted\Notifications\Notifiable
复制内容,并在自定义路径中创建一个新文件…例如App\Overrides\Notifications\Notifiable
您的文件将有两个更改…名称空间和您必须加载RoutesNotifications
trait,因为我们没有复制它
<?php
namespace App\Overrides\Notifications;
use use Illuminate\Notifications\RoutesNotifications;
trait Notifiable{
//The rest of the code remains
}
到目前为止,如果您使用的是mysql连接,这应该可以工作
要尝试此操作,请将用户模型上的应通知
特性更改为使用App\Overrides\Notifications\Notifiable
。通知将使用您指定的连接
MongoDB的用户将不得不采取额外的步骤,因为据我所知,尚未将其用于Laravel通知
因为这不是我们要问的问题,所以我们把它放在那里:-)基于@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;
}
非常简单,只需添加protected$connection='yourconnection NAME'代码>在<代码>照明\通知\数据库通知
就这些,它会起作用:)
如果要使用具有相同连接的一个通知表,则不需要创建新模型
如果您对用户模型使用不同的连接,我的代码将起作用。不要忘记也使用HasDatabaseNotification
,并在其中设置自定义DatabaseNotification
模型,否则它将不起作用;)在任何框架中都不应该更改核心文件。
<?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;
}
<?php
namespace App\Traits\Override;
use Illuminate\Notifications\RoutesNotifications;
trait Notifiable
{
use HasDatabaseNotifications, RoutesNotifications;
}
<?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');
}
}
<?php
namespace App\Models\Override;
use Illuminate\Notifications\DatabaseNotification as DatabaseNotification;
class MultiConnectionDatabaseNotification extends DatabaseNotification
{
// set your preferred connection here
protected $connection = 'oracle';
}