Hi,
I noticed that if you want to store your Notification in the database, and send them via toDatabase feature, there is an error, because of DatabaseNotification use Illuminate\Database\Eloquent\Model.
The first way to fix it was just replaced Jenssegers\Mongodb\Eloquent\Model in source files, but I sure such hard-coded way looks bad. Any ideas how to make it work without such hacking?
Thanks
Not sure what the best way to solve this is, but I managed to create a custom DatabaseNotification model that extends Jenssegers\Mongodb\Eloquent\Model. You then have to override the notifications relationship public function notifications(){} in your model to use your custom DatabaseNotification. That's pretty much it.
It worked for me! Thanks!!!
you just need to overwrite the Notification model and override the notifications function in all the MongoDB models that use Notifiable trait.
<?php
namespace App\Models;
use Illuminate\Notifications\DatabaseNotification;
class Notification extends DatabaseNotification
{
protected $connection = 'mysql';
}
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Jenssegers\Mongodb\Eloquent\Model;
class Chat extends Model
{
use Notifiable;
protected $collection = 'chats';
protected $connection = 'mongodb';
protected $guarded = [];
public function notifications()
{
return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc');
}
}
Most helpful comment
Not sure what the best way to solve this is, but I managed to create a custom
DatabaseNotificationmodel that extendsJenssegers\Mongodb\Eloquent\Model. You then have to override the notifications relationshippublic function notifications(){}in your model to use your customDatabaseNotification. That's pretty much it.