50 خطوط
1.3 KiB
PHP
50 خطوط
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Notification;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
public function index(): JsonResponse
|
|
{
|
|
$notifications = Notification::where('user_id', auth()->id())
|
|
->orderBy('created_at', 'desc')
|
|
->paginate(20);
|
|
|
|
return response()->json($notifications);
|
|
}
|
|
|
|
public function markRead(Notification $notification): JsonResponse
|
|
{
|
|
if ($notification->user_id !== auth()->id()) {
|
|
return response()->json(['message' => 'دسترسی غیرمجاز'], 403);
|
|
}
|
|
|
|
$notification->update(['is_read' => true]);
|
|
|
|
return response()->json($notification);
|
|
}
|
|
|
|
public function markAllRead(): JsonResponse
|
|
{
|
|
Notification::where('user_id', auth()->id())
|
|
->where('is_read', false)
|
|
->update(['is_read' => true]);
|
|
|
|
return response()->json(['message' => 'همه اعلانها خوانده شد']);
|
|
}
|
|
|
|
public function unreadCount(): JsonResponse
|
|
{
|
|
$count = Notification::where('user_id', auth()->id())
|
|
->where('is_read', false)
|
|
->count();
|
|
|
|
return response()->json(['count' => $count]);
|
|
}
|
|
}
|