# Adding column to Database Notifications table

Suppose your user can belong to many teams, and they have the ability to switch teams, you, like me, may want to allow the user to have unread notifications only for the team that they are currently in.

A similar situation may exist for multi-tenant applications where the same user can be a member of different tenants.

You may find that you need an additional column on the notifications table that you can then later filter with a global scope.

This article: <https://www.ystash.com/blog/extra-columns-with-laravel-database-notifications/> suggests creating a new database notification channel.

The approach presented here is simpler as it only involves listening to the Notification Eloquent model **creating** event using an [observer](https://laravel.com/docs/8.x/eloquent#observers).

### Add your additional column to the notifications table

This is fairly straightforward, just a regular migration targeting the notifications table

```php
    public function up()
    {
        Schema::table('notifications', function (Blueprint $table) {
            $table->foreignId('organisation_id');
        });
    }
```

### Create an observer

If you don't already have it, create app\Observers table

Create new file NotificationObserver.php

{% code title="app\Observers\NotificationObserver.php" %}

```php
<?php

namespace App\Observers;

class NotificationObserver
{
    public function creating($model)
    {
        // here set the column data for your new column
    }

}

```

{% endcode %}

### Add Observer to AppServiceProvider

In the boot() method of Providers/AppServiceProvider

```php
    public function boot()
    {
        DatabaseNotification::observe(NotificationObserver::class);
    }
```

Thats it! when your database notification is created, your additional data will be added


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://talltips.novate.co.uk/laravel/adding-column-to-database-notifications-table.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
