I am unable to get database notification option to work. No information is saved to the database, there's no error and nothing shows on Laravel Telescope.
This is my notification code:
<?php
namespace App\Notifications;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class LoginNotification extends Notification implements ShouldQueue
{
use Queueable;
/**
* The user instance.
*
* @var \App\Models\User
*/
public $user;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello ' .$this->user->first_name.',')
->line('There has been a login event on your account.')
->line('If this was you, no further action is required.')
->line('If not, please reset your password using the button below and consider enabling two-factor authentication on your account')
->action('Reset Password', url('/forgot-password'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'user' => $this->user->id,
'ip' => session('ip_address'),
];
}
}
The email gets sent but no data is saved to the notifications table. And only 'mail' is listed as a channel, 'database' isn't despite being there.
I'm at a loss on what the problem could be.
I got this to work by changing my queue connection to 'sync'. So the problem was because my queue connection was previously 'database'. I was using this and running a queue worker. So my question should be how to get toArray method to work when queue connection is set to database.
UPDATE: There's no issue here, all I needed to do was stop and restart the queue worker after my code changes.