Skip to content

Commit 481141f

Browse files
committed
Queued notifcations for User
1 parent e0b73b5 commit 481141f

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\User;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Queue\SerializesModels;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Bus\Dispatchable;
11+
12+
class ProcessUserNotification implements ShouldQueue
13+
{
14+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
15+
16+
public function __construct(private User $user, private mixed $instance)
17+
{}
18+
19+
/**
20+
* Execute the job.
21+
*
22+
* @return void
23+
*/
24+
public function handle()
25+
{
26+
$this->user->notify($this->instance);
27+
}
28+
}

app/Observers/UserObserver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Observers;
44

55
use App\Models\User;
6+
use App\Jobs\ProcessUserNotification;
67
use App\Notifications\User\UserCreated;
78
use App\Notifications\User\UserDeleted;
89

@@ -16,7 +17,7 @@ class UserObserver
1617
*/
1718
public function created(User $user)
1819
{
19-
$user->notify(new UserCreated);
20+
ProcessUserNotification::dispatch($user, new UserCreated);
2021
}
2122

2223
/**
@@ -27,6 +28,6 @@ public function created(User $user)
2728
*/
2829
public function deleted(User $user)
2930
{
30-
$user->notify(new UserDeleted);
31+
ProcessUserNotification::dispatch($user, new UserDeleted);
3132
}
3233
}

0 commit comments

Comments
 (0)