Display Notifications for different panels

Friend, I am still working on my exports with ExportAction between panels and the system using tenant, the problem for these cases is that in addition to the complex configurations for these cases, I am also not being able to show the database notifications in the corresponding panel... for testing What I am doing, I am able to generate an Excel document but the notification does not appear in the company panel, I checked several things, I even added a script in the panel for databaseNotifications but it does not work for me.
->databaseNotifications(function (\Illuminate\Database\Eloquent\Builder $query) {
// Asegurarte de usar el guard correcto
if (auth()->guard('company')->check()) {
$user = auth()->guard('company')->user();

// Filtrar las notificaciones del usuario logeado en el guard 'company'
return $query->where('customer_id', $user->id)
->where('notifiable_type', get_class($user));
}

// Por si no hay usuario autenticado, devolver un query vacío
return $query->whereRaw('1 = 0');
})
->databaseNotifications(function (\Illuminate\Database\Eloquent\Builder $query) {
// Asegurarte de usar el guard correcto
if (auth()->guard('company')->check()) {
$user = auth()->guard('company')->user();

// Filtrar las notificaciones del usuario logeado en el guard 'company'
return $query->where('customer_id', $user->id)
->where('notifiable_type', get_class($user));
}

// Por si no hay usuario autenticado, devolver un query vacío
return $query->whereRaw('1 = 0');
})
However, these do appear in the admin panel, the AdminPanel Any idea what I might be missing or wrong? Thank you very much friends!
No description
No description
No description
3 Replies
toeknee
toeknee4w ago
You can extend the notification class and add your own team_id or panel_id and then scope the notifications
TranceCode
TranceCodeOP4w ago
<?php

namespace App\Models\Company;

use App\Models\Company;
use Filament\Facades\Filament;
use Filament\Models\Contracts\HasTenants;
use Filament\Notifications\DatabaseNotification;
use Filament\Panel;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;

class Notification extends DatabaseNotification implements HasTenants
{
use HasFactory, Notifiable;
// Indica que el ID no es un entero sino un UUID
public $incrementing = false;
protected $keyType = 'string';

protected $fillable = [
'customer_id',
'type',
'data',
'read_at',
'notifiable_id',
'notifiable_type'
];

protected static function booted(): void
{
self::addGlobalScope('for_logged_company', function(Builder $builder):void {
if (auth()->guard('company')->check()) {
$user = auth()->user();
$companies = $user->companies;
if($companies->count() > 0) {
$company = $companies->first();
$companyId = $company->id;
}
}
});

self::saving(function (self $company): void {
if (auth()->guard('company')->check()) {
$user = auth()->user();
//dd($user);
$companies = $user->companies;
if ($companies->count() > 0) {
$company = $companies->first();
$company->company_id = $company->id;
}
}
});

self::creating(function (self $notification) {
if($tenant = Filament::getTenant()) {
$notification->customer_id = $tenant->id;
}
});
}

public function companies(): BelongsTo
{
return $this->belongsTo(Company::class, 'company_id');
}

public function getTenants(Panel $panel): array|Collection
{
return $this->companies;
}

public function canAccessTenant(Model $tenant): bool
{
return $this->companies->contains($tenant);
}
}
<?php

namespace App\Models\Company;

use App\Models\Company;
use Filament\Facades\Filament;
use Filament\Models\Contracts\HasTenants;
use Filament\Notifications\DatabaseNotification;
use Filament\Panel;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;

class Notification extends DatabaseNotification implements HasTenants
{
use HasFactory, Notifiable;
// Indica que el ID no es un entero sino un UUID
public $incrementing = false;
protected $keyType = 'string';

protected $fillable = [
'customer_id',
'type',
'data',
'read_at',
'notifiable_id',
'notifiable_type'
];

protected static function booted(): void
{
self::addGlobalScope('for_logged_company', function(Builder $builder):void {
if (auth()->guard('company')->check()) {
$user = auth()->user();
$companies = $user->companies;
if($companies->count() > 0) {
$company = $companies->first();
$companyId = $company->id;
}
}
});

self::saving(function (self $company): void {
if (auth()->guard('company')->check()) {
$user = auth()->user();
//dd($user);
$companies = $user->companies;
if ($companies->count() > 0) {
$company = $companies->first();
$company->company_id = $company->id;
}
}
});

self::creating(function (self $notification) {
if($tenant = Filament::getTenant()) {
$notification->customer_id = $tenant->id;
}
});
}

public function companies(): BelongsTo
{
return $this->belongsTo(Company::class, 'company_id');
}

public function getTenants(Panel $panel): array|Collection
{
return $this->companies;
}

public function canAccessTenant(Model $tenant): bool
{
return $this->companies->contains($tenant);
}
}
TranceCode
TranceCodeOP4w ago
I have created this Notification model and applied Database Notification extends, I understand that this is Filament's, my problem is how to use it? I had tried something like that!
use App\Models\Company\Notification as NotificationModel;

Tables\Actions\ExportAction::make()
->exporter(CommonAreaExporter::class)
->after(function () {
$tenant = Filament::getTenant();
// Crear la notificación manualmente
$notification = new NotificationModel([
'type' => 'export_completed',
'notifiable_id' => $tenant->id, // ID del tenant
'notifiable_type' => 'App\Models\Customer', // Tipo del modelo notifiable
'data' => json_encode(['message' => 'La exportación ha sido completada']),
'customer_id' => $tenant->id, // Customer relacionado
]);
$notification->save(); // Guardar en la base de datos
})
use App\Models\Company\Notification as NotificationModel;

Tables\Actions\ExportAction::make()
->exporter(CommonAreaExporter::class)
->after(function () {
$tenant = Filament::getTenant();
// Crear la notificación manualmente
$notification = new NotificationModel([
'type' => 'export_completed',
'notifiable_id' => $tenant->id, // ID del tenant
'notifiable_type' => 'App\Models\Customer', // Tipo del modelo notifiable
'data' => json_encode(['message' => 'La exportación ha sido completada']),
'customer_id' => $tenant->id, // Customer relacionado
]);
$notification->save(); // Guardar en la base de datos
})
I had tried something like this, but I get the error in the image, I understand that this method does not exist in Filament's DatabaseNotification class, so I can't understand what I should do or how to use the custom Notification class
No description
Want results from more Discord servers?
Add your server