Laravel-mongodb: DatabaseNotification supporting

Created on 29 Jan 2019  路  3Comments  路  Source: jenssegers/laravel-mongodb

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

Needs investigation

Most helpful comment

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.

All 3 comments

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.

model

<?php

namespace App\Models;


use Illuminate\Notifications\DatabaseNotification;

class Notification extends DatabaseNotification
{
    protected $connection = 'mysql';
}

overriding the notifications relation

<?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');
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

pirmax picture pirmax  路  3Comments

tomartailored picture tomartailored  路  3Comments

yupangestu picture yupangestu  路  3Comments

bastiendonjon picture bastiendonjon  路  3Comments

YSimple picture YSimple  路  3Comments