Notifications to database. #1525
Replies: 2 comments
-
I have something that works, but I am not sure exactly why. It turns out that the morphMany was not expecting a user, but a class which tells it which table to put the notifications in. I had to add to my custom BackupHasFailedNotification.php file.
Then to my CustomNotifiable.php class, I had to add
and
This should probably be DatabaseNotification:class, but I am getting data into the database now. just for clarity my config/backup.php now contains:
Obviously I still need to build the other Notifications. Any other suggestions to help would be appreciated. |
Beta Was this translation helpful? Give feedback.
-
I have been working on the same thing, i needed to send backup notifications via Database channel. Since the spatie/laravel-backup don't support database notification by default, i run into the same problems as you. So after few hours of research i found i gem article about "how notification channels work" written by this guy @ Mohamed Said which i highly recommend you to read it. So my solution was too simple, by following the official documentation of spatie/laravel-backup package (Adding extra notification channels) and (Customizing the notifiable) sections... The routeNotificationForDatabase method in your CustomNotifiable.php class should be something like this : use App\Models\User;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Spatie\Backup\Notifications\Notifiable;
class BackupNotifiable extends Notifiable
{
public function routeNotificationForDatabase(): MorphMany
{
return User::find(1)->notifications();
}
} In my case i want to notify the user with ID 1, for you it can be any Notifiable instance you want. The reason why this solution works because the However the /**
* Get the notification routing information for the given driver.
*
* @param string $driver
* @param \Illuminate\Notifications\Notification|null $notification
* @return mixed
*/
public function routeNotificationFor($driver, $notification = null)
{
if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) {
return $this->{$method}($notification);
}
return match ($driver) {
'database' => $this->notifications(),
'mail' => $this->email,
default => null,
};
} |
Beta Was this translation helpful? Give feedback.
-
I log my notifications to a database, so I can then send a single notification digest at the end of the day.
Trying to configure database notifications I first tried just exchanging
mail
todatabase
in the config. But the default notifications do not have a toArray or toDatabase method, so I created my own notification, with a toArray function. Now I am recieving an error message:I believe this is because the database notification is expecting a user to be passed to it, and that I would need to further extend
Spatie\Backup\Notifications\Notifiable
but besides adding a morphMany() function, I am not sure what it is looking for.Does anyone have any pointers?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions