1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers; |
3
|
|
|
|
4
|
|
|
use Illuminate\Http\JsonResponse; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\View\View; |
8
|
|
|
use Xetaravel\Models\User; |
9
|
|
|
|
10
|
|
|
class NotificationController extends Controller |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Constructor |
14
|
|
|
*/ |
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
parent::__construct(); |
18
|
|
|
|
19
|
|
|
$this->breadcrumbs->addCrumb('Notifications', route('users_notification_index')); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Show the notifications. |
24
|
|
|
* |
25
|
|
|
* @return \Illuminate\View\View |
26
|
|
|
*/ |
27
|
|
|
public function index(): View |
28
|
|
|
{ |
29
|
|
|
$user = User::find(Auth::user()->id); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->breadcrumbs->setCssClasses('breadcrumb'); |
32
|
|
|
|
33
|
|
|
$notifications = $user->notifications() |
34
|
|
|
->paginate(config('xetaravel.pagination.notification.notification_per_page')); |
35
|
|
|
$hasUnreadNotifications = $user->unreadNotifications->isNotEmpty(); |
36
|
|
|
|
37
|
|
|
return view( |
38
|
|
|
'notification.index', |
39
|
|
|
[ |
40
|
|
|
'user' => $user, |
41
|
|
|
'breadcrumbs' => $this->breadcrumbs, |
42
|
|
|
'notifications' => $notifications, |
43
|
|
|
'hasUnreadNotifications' => $hasUnreadNotifications |
44
|
|
|
] |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Delete a notification by its id. |
50
|
|
|
* |
51
|
|
|
* @param \Illuminate\Http\Request $request The current request. |
52
|
|
|
* |
53
|
|
|
* @return \Illuminate\Http\JsonResponse |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function delete(Request $request): JsonResponse |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$user = Auth::user(); |
58
|
|
|
$notification = $user->notifications() |
59
|
|
|
->where('id', $request->input('id')) |
60
|
|
|
->first(); |
61
|
|
|
|
62
|
|
|
if ($notification) { |
63
|
|
|
$notification->delete(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return response()->json([ |
67
|
|
|
'error' => false |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Mark a notification as read. |
73
|
|
|
* |
74
|
|
|
* @param \Illuminate\Http\Request $request The current request. |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\Http\JsonResponse |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function markAsRead(Request $request): JsonResponse |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$user = Auth::user(); |
81
|
|
|
$notification = $user->notifications() |
82
|
|
|
->where('id', $request->input('id')) |
83
|
|
|
->first(); |
84
|
|
|
|
85
|
|
|
if ($notification) { |
86
|
|
|
$notification->markAsRead(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return response()->json([ |
90
|
|
|
'error' => false |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Mark all notifications as read. |
96
|
|
|
* |
97
|
|
|
* @return \Illuminate\Http\JsonResponse |
98
|
|
|
*/ |
99
|
|
|
public function markAllAsRead(): JsonResponse |
100
|
|
|
{ |
101
|
|
|
$user = Auth::user(); |
102
|
|
|
$user->unreadNotifications->markAsRead(); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return response()->json([ |
105
|
|
|
'error' => false |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: