47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Notification;
|
|
use Illuminate\Http\Request;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$notifications = auth()->user()->notifications()
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
// Tandai semua sebagai sudah dibaca saat halaman dibuka
|
|
auth()->user()->notifications()->where('is_read', false)->update(['is_read' => true]);
|
|
|
|
return view('notifications.index', compact('notifications'));
|
|
}
|
|
|
|
public function markRead($id)
|
|
{
|
|
$notif = Notification::where('id', $id)
|
|
->where('user_id', auth()->id())
|
|
->firstOrFail();
|
|
|
|
$notif->update(['is_read' => true]);
|
|
|
|
return back();
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
Notification::where('id', $id)
|
|
->where('user_id', auth()->id())
|
|
->delete();
|
|
|
|
return back()->with('status', 'Notifikasi dihapus.');
|
|
}
|
|
|
|
public function destroyAll()
|
|
{
|
|
auth()->user()->notifications()->delete();
|
|
return back()->with('status', 'Semua notifikasi dihapus.');
|
|
}
|
|
} |