Column 'notifiable_id' cannot be null - Laravel Database Notif

Viewed 18

I create laravel notification that record on database. But face problem when try to add record and send notif to table notifications.

Migration

Schema::create('notifications', function (Blueprint $table) {
   $table->uuid('id')->primary();
   $table->string('type');
   $table->morphs('notifiable');
   $table->text('data');
   $table->timestamp('read_at')->nullable();
   $table->timestamps();
});

NewSiteNotification

class NewSiteNotification extends Notification
{
    use Queueable;
    public $emailData;

    public function __construct($emailData)
    {
        $this->emailData = $emailData;
    }

    public function via($notifiable)
    {
        return ['database'];
    }

    public function toArray($notifiable)
    {
        return [
            'SUBSCRIBER_NAME' => $this->emailData->SUBSCRIBER_NAME,
            'SUBSCRIBER_NUMBER' => $this->emailData->SUBSCRIBER_NUMBER,
        ];
    }
}

Controller

$emailData = // DATA
_______________
 $admins = User::join('ADM_TRX_USERROLE as UR', 'UR.ADM_MST_USER_ID', 'ADM_MST_USER.ID')
   ->whereIn('UR.ADM_MST_ROLE_ID', [31, 114])
   ->select([
      'ADM_MST_USER.ID', 'ADM_MST_USER.USERNAME'
    ])
   ->get();
                    
 Notification::send($admins, new NewSiteNotification($emailData));

Error

"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'notifiable_id' cannot be null (SQL: insert into `notifications` (`id`, `type`, `data`, `read_at`, `notifiable_id`, `notifiable_type`, `updated_at`, `created_at`) values (0c9b6b29-afea-4b03-81b8-cfb05e834566, App\\Notifications\\NewSiteNotification, {\"SUBSCRIBER_NAME\":\"TEST NOTIF 2\",\"SUBSCRIBER_NUMBER\":\"NOTIF002\"}, ?, ?, App\\Models\\User, 2022-09-14 15:55:42, 2022-09-14 15:55:42))"
}

I'll appreciate every answer. Thank you

1 Answers

Error could be caused by notifiable_id not in the $fillable property. If that doesn't solve the issue, assuming notifiable_id is not stated in your form, then nullableMorphs rather than morphs type for your migration should fix the issue.

Related