9

I'm trying to create a notification system for my social network. But I'm stuck on the database design. so when a user comments on another user I want to display "X commented on your post." or when someone follows another I want to display a notification.

This is my table so far:

CREATE TABLE IF NOT EXISTS `notifications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `notification_id` int(11) NOT NULL,
  `text` text NOT NULL,
  `read` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

The part where I'm confused at is, Should I insert a record when someone follows another person? Like... Right after when someone clicks follow button? If so, What should I insert to the row?

And I'm using PHP with Laravel framework.

3
  • You also need "followers" table (many to many relationship: id/user_id/follower_id with composite unique key on user,follower column) - this is where you insert records about new follower. Btw. Field text is redundant and notification_id should rather be comment_id (+ some comment_type if comments/posts/... notifications are sent for various entities).
    – shudder
    Apr 3, 2015 at 18:36
  • I have a followers table. I don't know when to insert it?
    – Akar
    Apr 3, 2015 at 18:40
  • When someone clicks "follow that user" maybe?
    – shudder
    Apr 3, 2015 at 18:42

4 Answers 4

20

Since this is an open-ended question with no perfect solution, I would recommend the following structure:

Events table

id    type          text
-----------------------------------------------
1     comment       commented on your post
2     follow        followed you

etc

Then the notification table would be as follows (I couldn't think of better field names to convey the idea):

Notification table

id      user_to_notify       user_who_fired_event  event_id    seen_by_user
--------------------------------------------------------------------------- 
 1            12                 13                    2        yes
 2            13                 12                    1        no
 3            1                  15                    1        yes

seen_by_user could be boolean.

The advantage of using the above structure is you can answer the following queries without complex SQL queries:

  1. Total comments on user X's post by user Y.
  2. The pattern of users reading or clicking on notifications. e.g., he may not click on the notifications of people following him.
  3. If a user has given a preference not to be notified about certain events, you can filter it out based on event_id.

As soon as X comments on Y's post, you make an entry in the notification table and then an entry into the comments table with the comments. I hope this helps in some way!

3
  • 1
    Notification table should be added a time field, for example, the notification message shows xxxx minutes ago.
    – Terry Lin
    Aug 14, 2016 at 14:47
  • 1
    although your approach is simple, but it does not fulfill some use cases. for example, we know that there will be a comments table! and now what if i want to store comment_id or post_id and etc in the notifications table!? this is useful a lot of times. for example, when i want to say "John replied on your comment 'love you man', xx minutes ago." and i want to link it to the post where comment lives! what about that? though your approach is really simplistic, but it misses a lot of features that are needed lots of times
    – Moher
    Dec 14, 2017 at 22:42
  • 5
    @Moher One way to tackle that issue would be to have a column called "trigger_id" or "source_id" in notification table. This will refer to the post/comment/any event that triggered this event. Also, this is the most basic thing, and a lot of columns will have to be added to make it production ready. The idea was to give a general direction of thinking!
    – Ymartin
    Jan 3, 2018 at 10:00
3

i know i'm very late but for those who still searching for.. i'm building something similar and i'm using this and it's working fine for me

    public function up()
{
    Schema::create('notifications', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('user_id'); // set relationship in user model 
        $table->string('user_to_notify'); //the user who will recive this notification
        $table->string('type'); //follow ,comments etc
        $table->string('data'); //follow id ,comment id etc ,you can set relationship in model about this
        $table->integer('read'); 
        $table->timestamps();
    });
}
1

For anyone who is using Laravel 5.4 or above (I'm not sure on lower versions) :

you can type php artisan notifications:table and it will create a migration with the basic fields you need to use with the Laravel Notifications.

more info at https://laravel.com/docs/5.5/notifications

0

I suggest something like this:

Table person (id, name, etc)

Table post (id, personid, text, etc)

Table comment (id, personid, postid, text, SeenByOP, etc)

SeenByOp will be a bit field with a default value of 0. When the OP looks at the comment, the value is updated to 1.

I see no need for a notifications table.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.