-
Notifications
You must be signed in to change notification settings - Fork 17
/
Megaphone.php
54 lines (41 loc) · 1.23 KB
/
Megaphone.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace MBarlow\Megaphone\Livewire;
use Illuminate\Http\Request;
use Illuminate\Notifications\DatabaseNotification;
use Livewire\Component;
class Megaphone extends Component
{
public $user;
public $announcements;
public $unread;
public $showCount;
public $rules = [
'unread' => 'required',
'announcements' => 'required',
];
public function mount(Request $request)
{
$this->user = $request->user();
$this->loadAnnouncements($this->user);
$this->showCount = config('megaphone.showCount', true);
}
public function loadAnnouncements($user)
{
$this->unread = $this->announcements = collect([]);
if ($user === null || get_class($user) !== config('megaphone.model')) {
return;
}
$announcements = $user->announcements()->get();
$this->unread = $announcements->whereNull('read_at');
$this->announcements = $announcements->whereNotNull('read_at');
}
public function render()
{
return view('megaphone::megaphone');
}
public function markAsRead(DatabaseNotification $notification)
{
$notification->markAsRead();
$this->loadAnnouncements($this->user);
}
}