php - Laravel - Storing notification settings per user with different channels

one text

Solution:

I think a many-to-many relationship would be more suited to this.

Tables:

User
 - id

Notifications
 - id

NotificationUser <-- pivot table
 - notifcation_id
 - user_id
 - channel_id

Channel
 - id
 - name 

To account for these additional fields in the pivot table, define them in the User model relationship:

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function notifications()
    {
        return $this->belongsToMany(Notification::class)->withPivot(['channel_id']);
    }
}

See: https://laravel.com/docs/8.x/eloquent-relationships#retrieving-intermediate-table-columns

This way, you can advantage of the relationship methods that Laravel (eloquent) comes with.

i.e:

aUser->notifications(); # Getting a list of a user's notifications
aUser->attach(1, ['channel' => 1]); # attaching a notification to the user

You can also take advantage of query scopes to retrieve one channel of notifications for the user etc

See: https://laravel.com/docs/8.x/eloquent#query-scopes

Then use the model/listener pattern as you had planned.

Source