🤹
TALL Stack Tips
  • What is TALL Stack
  • Tailwind
    • Tailwind Resources
    • Swinging Bell Notification Icon
    • Styled Unordered Lists
  • Alpine
  • Alpine Resources
  • Tabbed Content Using Alpine JS
  • Checkbox component with SVG tick
  • Dropdown animation
  • Create a Sliding Puzzle Captcha
  • Laravel
    • Tabler Icons Component
    • Password-less Login with Laravel 8+
    • Password-less Login with Magic Link in Laravel 8
    • Laravel Resources
    • Laravel Breeze Login Conditional Redirect
    • Jetstream Login Conditional Redirect
    • Simplify Laravel CRUD Controllers
    • CSRF and expired login forms
    • CSRF and expired logout forms
    • Add your own logo to Laravel Mail
    • Specify a different mail theme for Notifications
    • Show custom page when email verification link expired
    • Using a mutator to save currency
    • Using Spatie Valuestore to hold frequently accessed settings
    • Using the old() helper
    • Alternatives to using Eloquent Accessor
    • UpdateOrCreate may not update timestamp
    • Use of lockForUpdate()
    • Using S3
    • Super Simple User Based Feature Flags
    • Installing a Specific Version of Laravel
    • Versioning your Laravel Project
    • CSS Cache Busting with your Git Commit SHA
    • Adding column to Database Notifications table
    • Find nearby locations using the Haversine formula in Eloquent query
    • Using Queues on Shared Hosting with Laravel
    • Create Guaranteed Unique Invoice Number in Laravel
    • Send Notification to all team members
    • Protect Staging site with Basic Auth
    • Working with Enums
    • PHP DateTime formatting cribsheet
  • Livewire
    • Livewire Resources
    • Naming Livewire Components
    • Dynamic Cascading Dropdown with Livewire
    • Hiding a button after click
    • Working with Javascript Components
    • SweetAlert2 with Livewire
    • Select Multiple or Checkboxes
    • Clearing checkboxes in Livewire
    • Livewire File Uploads Using S3
    • Simple Log File Viewer
  • Related Resources
    • Testing resources
    • When Composer runs out of memory
    • Deployment
    • Security
    • Scheduler & Cron tips
    • LastPass tips
    • Using Git
    • VSCode Tips
    • Markdown
    • Cpanel resources
Powered by GitBook
On this page
  • Add your additional column to the notifications table
  • Create an observer
  • Add Observer to AppServiceProvider

Was this helpful?

  1. Laravel

Adding column to Database Notifications table

When you need additional scope on notifications

PreviousCSS Cache Busting with your Git Commit SHANextFind nearby locations using the Haversine formula in Eloquent query

Last updated 3 years ago

Was this helpful?

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: 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 .

Add your additional column to the notifications table

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

    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

app\Observers\NotificationObserver.php
<?php

namespace App\Observers;

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

}

Add Observer to AppServiceProvider

In the boot() method of Providers/AppServiceProvider

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

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

https://www.ystash.com/blog/extra-columns-with-laravel-database-notifications/
observer