Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kkatwork's avatar

Notification table doesn't exist error

Hi,

I am facing a strange issue in my project.

I have a separate database for my 'users' table. I want to use notifications for the users.

class User extends Model {
protected $connection = 'myusers';
}

When creating the notification using

$user->notify(new EmailSent()); 

Laravel searches for 'notfications' table in the database where user table is located. So I migrated notifications table to the 'myusers' database.

But, when I try to get notifications of user

auth()->user()->unreadNotificatins;

This time, laravel searches for notification table in my default connection and throws table not found exception.

Why is this ? When storing notification table requires in Users connection and when reading it can't read from there? When I specify the connection property pointing to notification table in DatabaseNotifications class it works well But it should take automatically from User Model as it reads when creating new notification.

Is it a bug in Laravel? Or Am I doing something wrong

I hope question is understandable. Please answer as I don't want to edit code in vendor directory.

0 likes
5 replies
MichalOravec's avatar
Level 75

Create own model for notifications

<?php

namespace App\Models;

use Illuminate\Notifications\DatabaseNotification;

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

And in the User model replace default relationship with your own model for notifications

<?php

namespace App\Models;

use App\Models\Notification;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    protected $connection = 'myusers';

    /**
     * Get the entity's notifications.
     */
    public function notifications()
    {
        return $this->morphMany(Notification::class, 'notifiable');
    }
}
1 like
MichalOravec's avatar

No, it shouldn't because it uses a default connection and you need to replace it.

kkatwork's avatar

@michaloravec I think it uses correct connection when using $user->notify(); and default when using same user class $user->notifications; ! thanks.

Please or to participate in this conversation.