F
Filament15mo ago
TiBiBa

Notifications works when triggered on mount() but not when triggered on submit()?

Hi! I'm stuck on showing notifications in the panel builder. I'm trying to show a notification to the user to inform them that no results are found for their search query. Using the following approach on a custom page:
public function submit(): void
{
Notification::make()
->title('This should be shown on submit')
->warning()
->send();

// dd('We get here!');
}
public function submit(): void
{
Notification::make()
->title('This should be shown on submit')
->warning()
->send();

// dd('We get here!');
}
The notification is never shown but when I uncomment the dd() this is executed as expected. The weird thing is that the notifications seem to work fine on the mount() function. The following snippets shows a notification when re-loading the page:
public function mount(): void
{
Notification::make()
->title('This is a test notifications')
->warning()
->send();
$this->form->fill([]);
}
public function mount(): void
{
Notification::make()
->title('This is a test notifications')
->warning()
->send();
$this->form->fill([]);
}
Thanks in advance!
14 Replies
LeandroFerreira
LeandroFerreira15mo ago
Have you ever posted this issue before?
TiBiBa
TiBiBaOP15mo ago
I did, but couldn’t find an answer and I’m unable to find the old question within this channel.
LeandroFerreira
LeandroFerreira15mo ago
Can you share the project on github?
TiBiBa
TiBiBaOP15mo ago
Unfortunately it’s a corporate project and I’m unable to share the whole project. I can provide required code snippets though.
LeandroFerreira
LeandroFerreira15mo ago
maybe you can share the custom page.. the whole code
TiBiBa
TiBiBaOP15mo ago
The issue occurs on all pages, also the default resource ones. I made a new custom page that reproduces the error. But once again, it seems the issue is globally.
<?php

namespace App\Admin\Pages;

use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;

class NotificationTest extends Page implements HasForms {

use InteractsWithForms;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'admin.pages.notification-test';

public function mount(): void
{
Notification::make()
->title('This notification works fine!')
->warning()
->send();

$this->form->fill([]);
}

public function submit(): void
{
Notification::make()
->title('No results found for your search query')
->warning()
->send();

// The dd() below works fine when we uncomment the line
// dd('We get here!');
}
}
<?php

namespace App\Admin\Pages;

use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;

class NotificationTest extends Page implements HasForms {

use InteractsWithForms;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'admin.pages.notification-test';

public function mount(): void
{
Notification::make()
->title('This notification works fine!')
->warning()
->send();

$this->form->fill([]);
}

public function submit(): void
{
Notification::make()
->title('No results found for your search query')
->warning()
->send();

// The dd() below works fine when we uncomment the line
// dd('We get here!');
}
}
LeandroFerreira
LeandroFerreira15mo ago
Is there something in the AppServiceProvider? Are you using plugins as well?
TiBiBa
TiBiBaOP15mo ago
This is the appPanelProvider
LeandroFerreira
LeandroFerreira15mo ago
this app/Providers/AppServiceProvider.php
TiBiBa
TiBiBaOP15mo ago
This is a bit of a complex file were we decide if the provider we need is the admin or any of our other applications with something like this:
private function _getApplicationProvider() {
if ($isAdmin) {
// Set app type to admin (for when local)
config(['app.type' => 'admin']);
return self::$ApplicationTypes['admin'];
}

if (config('app.type')) {
return self::$ApplicationTypes[config('app.type')];
}

return self::$ApplicationTypes['webapp'];
}
private function _getApplicationProvider() {
if ($isAdmin) {
// Set app type to admin (for when local)
config(['app.type' => 'admin']);
return self::$ApplicationTypes['admin'];
}

if (config('app.type')) {
return self::$ApplicationTypes[config('app.type')];
}

return self::$ApplicationTypes['webapp'];
}
Then the AdminServiceProvider registers the appPanelProvider
LeandroFerreira
LeandroFerreira15mo ago
probably an issue in your project, not Filament. Maybe you can duplicate the project, remove all resources, infos, etc, and share with us on github
TiBiBa
TiBiBaOP15mo ago
I tried, but unfortunately the codebase is too complex to separate en share publicly. I did find some more info. When rendering a page the session has some attributes (as shown in the debugbar) but when I submit a form the session seems to be emptied?
Diogo Pinto
Diogo Pinto15mo ago
I don’t know if I understood your question correctly, but wouldn’t it be easier to make a form with an action? You would add an action that queries the DB for the $state and if no results are found it triggers a notification? Else it passes the collections to the results?
TiBiBa
TiBiBaOP15mo ago
The question is independent of how the forms works. It's a general issue with displaying notifications. For an unknown reason they are displayed as expected before the mount(), but afterwards not. I finally managed to fix this. Long story short: We defined custom middleware groups to completely separate between our admin panel and other application. Due to this change the web middleware was still empty. The notifications issue was solved by adding \Illuminate\Session\Middleware\StartSession::class to the empty web array.
Want results from more Discord servers?
Add your server