Notifications

Guys, have you ever had a problem with notifications? I'm making an insertion in my customers table and I needed a notification to be displayed when registered. Registration takes place through an end-point on a route that I created to receive an external request. Reading the documentation I saw databaseNotifications. There in my AdminPanelProvider I placed the property: ->databaseNotifications(); So far, it was a great show, it ran smoothly and appeared on the panel. Then I ran the notification migration, everything was fine too. I created a CustomerObserver observer, and in the creation function I did:
public function created(Customer $customer): void
{
Notification::make()
->title('Saved successfully')
->body('Customer created successfully')
->sendToDatabase($customer);
}
public function created(Customer $customer): void
{
Notification::make()
->title('Saved successfully')
->body('Customer created successfully')
->sendToDatabase($customer);
}
Also, in my EventServiceProvider in the method:
public function boot(): void
{
Customer::observe(CustomerObserver::class);
}
public function boot(): void
{
Customer::observe(CustomerObserver::class);
}
I made this modification. However, despite everything, the notification was not registered in the bd nor displayed.
35 Replies
Dennis Koch
Dennis Koch4mo ago
Please read our #✅┊rules and format your code propely
walyson_
walyson_OP4mo ago
Sorry
Dennis Koch
Dennis Koch4mo ago
You are sending the notification to the customer that was created. Is this intentional? ->sendToDatabase($customer); It doesn't make sense with the current title and body, but maybe you want to send a "welcome" notification? Are customers the model that logs in to your panel?
walyson_
walyson_OP4mo ago
Sorry, The correct thing would be:
->sendToDatabase(auth()->user());
->sendToDatabase(auth()->user());
We can ignore the texts for now, I'm doing tests first. When I make the deletion through the table action, the notification works. However, when registration is done through the end-point, it is not displayed.
Dennis Koch
Dennis Koch4mo ago
1. Did you check created() is called? 2. Is there an authenticated user?
walyson_
walyson_OP4mo ago
2. Is there an authenticated user? Yes. image of my class:
No description
Dennis Koch
Dennis Koch4mo ago
What's the result you get when you add dd(auth()->user()); inside the created() method? 3. Do you see a record in the notifications table?
walyson_
walyson_OP4mo ago
The deletion notification works when I delete it through the action.
No description
Dennis Koch
Dennis Koch4mo ago
Can we please keep the conversation in english? 😅
walyson_
walyson_OP4mo ago
I put a dd() inside create and it didn't fire.
Dennis Koch
Dennis Koch4mo ago
Well then something is wrong with your observer 😅
LeandroFerreira
LeandroFerreira4mo ago
are you using queue? php artisan queue:work ?
walyson_
walyson_OP4mo ago
No, I didn't implement a queue
LeandroFerreira
LeandroFerreira4mo ago
what is the value from QUEUE_CONNECTION in .env file?
walyson_
walyson_OP4mo ago
sync
LeandroFerreira
LeandroFerreira4mo ago
laravel version?
walyson_
walyson_OP4mo ago
Laravel Framework 10.48.14
LeandroFerreira
LeandroFerreira4mo ago
try to add in your Customer model
use Illuminate\Database\Eloquent\Attributes\ObservedBy;

#[ObservedBy(CustomerObserver::class)]
class Customer extends Model...
use Illuminate\Database\Eloquent\Attributes\ObservedBy;

#[ObservedBy(CustomerObserver::class)]
class Customer extends Model...
walyson_
walyson_OP4mo ago
I put it on but it didn't work
LeandroFerreira
LeandroFerreira4mo ago
are you sure that is it creating the record?
walyson_
walyson_OP4mo ago
The strange thing is that the deletion works. However, it is done through the filament panel Yes, I'm checking the table
LeandroFerreira
LeandroFerreira4mo ago
customers and notifications tables?
walyson_
walyson_OP4mo ago
Only in the customer table.
LeandroFerreira
LeandroFerreira4mo ago
weird.. could you try to send the notification using afterCreate hook? https://filamentphp.com/docs/3.x/panels/resources/creating-records#lifecycle-hooks just to see if it works..
LeandroFerreira
LeandroFerreira4mo ago
did you try something like Log::info($message); in the created method and check the laravel.log?
walyson_
walyson_OP4mo ago
#59 {main}"}
[2024-08-02 18:16:19] local.INFO: Customer created successfully
[2024-08-02 18:16:19] local.ERROR: Filament\Notifications\Notification::sendToDatabase(): Argument #1 ($users) must be of type Illuminate\Database\Eloquent\Model|Illuminate\Contracts\Auth\Authenticatable|Illuminate\Support\Collection|array, null given, called in C:\me2\painel-agro-score\app\Observers\CustomerObserver.php on line 20 {"exception":"[object] (TypeError(code: 0): Filament\\Notifications\\Notification::sendToDatabase(): Argument #1 ($users) must be of type Illuminate\\Database\\Eloquent\\Model|Illuminate\\Contracts\\Auth\\Authenticatable|Illuminate\\Support\\Collection|array, null given, called in C:\\me2\\painel-agro-score\\app\\Observers\\CustomerObserver.php on line 20 at C:\\me2\\painel-agro-score\\vendor\\filament\\notifications\\src\\Notification.php:177)
[stacktrace]
#59 {main}"}
[2024-08-02 18:16:19] local.INFO: Customer created successfully
[2024-08-02 18:16:19] local.ERROR: Filament\Notifications\Notification::sendToDatabase(): Argument #1 ($users) must be of type Illuminate\Database\Eloquent\Model|Illuminate\Contracts\Auth\Authenticatable|Illuminate\Support\Collection|array, null given, called in C:\me2\painel-agro-score\app\Observers\CustomerObserver.php on line 20 {"exception":"[object] (TypeError(code: 0): Filament\\Notifications\\Notification::sendToDatabase(): Argument #1 ($users) must be of type Illuminate\\Database\\Eloquent\\Model|Illuminate\\Contracts\\Auth\\Authenticatable|Illuminate\\Support\\Collection|array, null given, called in C:\\me2\\painel-agro-score\\app\\Observers\\CustomerObserver.php on line 20 at C:\\me2\\painel-agro-score\\vendor\\filament\\notifications\\src\\Notification.php:177)
[stacktrace]
While debugging I found this guy. Apparently auth()->user() is arriving as null
LeandroFerreira
LeandroFerreira4mo ago
are you using a different guard? did you try using afterCreate? What is the output from dd(auth()->id()) ?
walyson_
walyson_OP4mo ago
After Create works even when registration is done externally to the filament?
LeandroFerreira
LeandroFerreira4mo ago
no are you trying to use this observer in the registration form?
walyson_
walyson_OP4mo ago
My insertion is done through an external request. Create a controller to receive this information and then register. I want to use notification just to say that it has been registered. No
LeandroFerreira
LeandroFerreira4mo ago
are you sure that the user is logged? if it is external, how auth()->user() will work?
walyson_
walyson_OP4mo ago
I believe this is exactly the problem. Even though the user is logged into the panel, the post is external, that is, it will not auth()->user()
LeandroFerreira
LeandroFerreira4mo ago
No, this won’t work. auth()->user() retrieves the authenticated user instance, but it will only work inside your application, not outside of it.
walyson_
walyson_OP4mo ago
Any suggestion?
LeandroFerreira
LeandroFerreira4mo ago
If you are using a service to handle this, you should also specify who will receive the notification
Want results from more Discord servers?
Add your server