UserAccessLogService.php 796 B

1234567891011121314151617181920212223242526272829
  1. <?php
  2. namespace App\Services;
  3. use App\Models\UserAccessLog;
  4. use Illuminate\Pagination\LengthAwarePaginator;
  5. class UserAccessLogService
  6. {
  7. public function getAllPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
  8. {
  9. $query = UserAccessLog::with(['user:id,name,type'])
  10. ->orderBy('accessed_at', 'desc');
  11. if (!empty($filters['type'])) {
  12. $query->whereHas('user', fn($q) => $q->where('type', $filters['type']));
  13. }
  14. if (!empty($filters['date_from'])) {
  15. $query->whereDate('accessed_at', '>=', $filters['date_from']);
  16. }
  17. if (!empty($filters['date_to'])) {
  18. $query->whereDate('accessed_at', '<=', $filters['date_to']);
  19. }
  20. return $query->paginate($perPage);
  21. }
  22. }