如何保存用户的通知设置laravel5和mysql [关闭]

I am building Restful Api for a mobile application. I need to push the notification to user if he has saved the notification setting to true in his profile.

I have created the following structure:

Users Table:

id name email

1 Test test@test.com

2 Test1 test1@test1.com

Notifications Table:

id notification_name

1 Like Notification

2 Comment Notification

user_notification Table:

id notification_id user_id value (0=off, 1=on)

1            1                 1         0   

2            2                 1         1   

3            1                 1         1   

4            2                 1         1   

In the above table structure I have created a pivot table with an extra field value for saving settings of a user.

Is there any another great way to do this, in which I can minimize my queries and respond fast

Your pivot table should only contain the two references defined, notification_id and user_id. Adding more fields to that negates it as a pivot table.

You shpuld put the value field on your notifications table instead, perhaps naming it better like is_active. It is your notification that has a setting, right? So it makes sense that the setting goes on the notification, not the pivot table which only serves to connect the two tables.

Also, as a sugestion, have your pivot table alphabetical as per normal convention in Laravel (see the docs)

In your User.php model, add the relationship

public function notifications() {
    return $this->hasMany(App\Notification::class, 'notification_user');
}

When querying this relationship, all you have to do is:

foreach($user->notifications as $notification) {
    if($notification->is_active) {
        // do something
    }
}