50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
/**
|
|
* Mendapatkan daftar notifikasi user saat ini.
|
|
*/
|
|
public function index()
|
|
{
|
|
$user = Auth::user();
|
|
|
|
if (!$user) {
|
|
return response()->json(['success' => false, 'message' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
// Ambil notifikasi (semua), urutkan yang terbaru, batasi 20
|
|
$notifications = $user->notifications()->latest()->take(20)->get();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $notifications
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Menandai semua notifikasi sebagai telah dibaca.
|
|
*/
|
|
public function markAsRead()
|
|
{
|
|
$user = Auth::user();
|
|
|
|
if (!$user) {
|
|
return response()->json(['success' => false, 'message' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
$user->unreadNotifications->markAsRead();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Semua notifikasi ditandai telah dibaca.'
|
|
]);
|
|
}
|
|
}
|