siakad-itm/app/Services/NotificationService.php
Yoga Pangestu 0397959f60
Some checks failed
tests / ci (pull_request) Has been cancelled
feat: implement notification system with CRUD operations and UI integration
2026-08-25 13:21:11 +07:00

51 lines
1.2 KiB
PHP

<?php
namespace App\Services;
use App\Models\Notification;
use App\Models\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
class NotificationService
{
public function paginated(User $user, int $perPage = 15): LengthAwarePaginator
{
return Notification::query()
->where('user_id', $user->id)
->orderByDesc('created_at')
->orderByDesc('id')
->paginate($perPage);
}
public function unreadCount(User $user): int
{
return Notification::query()
->where('user_id', $user->id)
->where('is_read', false)
->count();
}
public function markAsRead(Notification $notification): void
{
$notification->update(['is_read' => true]);
}
public function markAllAsRead(User $user): void
{
Notification::query()
->where('user_id', $user->id)
->where('is_read', false)
->update(['is_read' => true]);
}
public function delete(Notification $notification): bool
{
return $notification->delete();
}
public function deleteAll(User $user): void
{
Notification::query()->where('user_id', $user->id)->delete();
}
}