0

I'm implementing the Laravel notification system. I have made a test of insertion in DB and it has gone well but when registering the notification I see that it arrives with "strange" data (from my point of view).

id type notifiable_type notifiable_id
634db0ee-a9c0... App\Notifications\HeatMapNotification App\Models\User 1

I don't understand why "id" column comes with an id with type uuid. I mean, with an autoincremental id would be enough (I think).

In the "type" column I would like to insert a much simpler text.

I don't see any use for the column "notifiable_type". In my case the notifiable entities will always be users.

Also I would like to change some column names (notifiable_id --> user_id)

How could I make these changes?

Thank you very much in advance.

4
  • You could switch to using your own notification system that is essentially a copy of Laravel's or you could just leave it, which is what I recommend. You never know, you might want to notify something else in the future. Commented Apr 30, 2021 at 12:43
  • @kemp yes, I understand you, but I find it very annoying to see text strings like "App\Notifications\HeatMapNotification" when I really just want to know the type of entity ("heat_map"), and even others like the type of notifiable entity. When could it be something other than a user? The uuid column type also has no sense for me. I really don't understand the way they thought when they made this table. Commented Apr 30, 2021 at 12:50
  • They are trying to provide the most flexibility. Consider an application which has two types of users: doctors and patients. They both log into the system and can receive notifications. Also, what if you put your notifications in subdirectories? App\Notifications\Appointment\Reminder and App\Notifications\Prescription\Reminder would both be called reminder. So, why not just store the full class name? Commented Apr 30, 2021 at 13:00
  • @kemp, yeah, you're right. Anyway, it's not neccessary in my case so I'll just wait for help. Thanks for the tip kemp :) Commented Apr 30, 2021 at 13:06

1 Answer 1

1

See at the DatabaseNotification class and see some of your requirements. For example by default have

  /**
     * The "type" of the primary key ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

maybe working on this you can enable the autoincrement ID. In DatabaseChannel class is when store some data in notifications table

protected function buildPayload($notifiable, Notification $notification)
    {
        return [
            'id' => $notification->id,
            'type' => get_class($notification),
            'data' => $this->getData($notifiable, $notification),
            'read_at' => null,
        ];
    }

I think about the type column, it's easier cast the notification type

if ($notification->type === HeatMapNotification::class)

and notifiable_type, you can manage a lot of diff User models...depend of the project you dev, so Laravel go into this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.