feat: optimize user queries in AdministratorService, LecturerService, and StudentService for better performance
Some checks failed
tests / ci (pull_request) Has been cancelled

This commit is contained in:
Yoga Pangestu 2026-08-30 11:35:10 +07:00
parent 7bdfd25adb
commit 1b5367c78a
4 changed files with 32 additions and 7 deletions

View File

@ -11,7 +11,11 @@ class AdministratorService
{
public function paginated(int $perPage = 25, string $search = '', ?string $gender = null): LengthAwarePaginator
{
return User::with(['profile', 'roles'])
return User::select(['id', 'username', 'email', 'is_active'])
->with([
'profile:id,user_id,full_name,phone_number,gender',
'roles:id,name',
])
->whereHas('roles', fn ($q) => $q->whereIn('name', ['staff-admin', 'staff-keuangan']))
->when($search, fn ($q) => $q->where(function ($query) use ($search) {
$query->where('username', 'like', "%{$search}%")

View File

@ -14,7 +14,12 @@ class LecturerService
{
public function getAllForSelect(): Collection
{
return Lecturer::with('user.profile')->get();
return Lecturer::select(['id', 'user_id', 'lecturer_number'])
->with([
'user:id,username',
'user.profile:id,user_id,full_name',
])
->get();
}
public function paginated(int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null): LengthAwarePaginator
@ -33,7 +38,12 @@ public function forExport(string $search = '', ?string $gender = null, ?int $dep
private function filteredQuery(string $search, ?string $gender, ?int $departmentId): Builder
{
return User::with(['profile', 'lecturer.department'])
return User::select(['id', 'username', 'email', 'is_active'])
->with([
'profile:id,user_id,full_name,phone_number,gender,birth_place,birth_date,address',
'lecturer:id,user_id,lecturer_number,department_id',
'lecturer.department:id,name',
])
->whereHas('roles', fn ($q) => $q->where('name', 'dosen'))
->when($search, fn ($q) => $q->where(function ($query) use ($search) {
$query->where('username', 'like', "%{$search}%")

View File

@ -14,7 +14,13 @@ class StudentService
{
public function getAllForSelect(): Collection
{
return Student::with(['user.profile', 'department'])->get();
return Student::select(['id', 'user_id', 'student_number', 'department_id'])
->with([
'user:id,username',
'user.profile:id,user_id,full_name',
'department:id,name',
])
->get();
}
public function paginated(int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null): LengthAwarePaginator
@ -33,7 +39,12 @@ public function forExport(string $search = '', ?string $gender = null, ?int $dep
private function filteredQuery(string $search, ?string $gender, ?int $departmentId, ?string $status): Builder
{
return User::with(['profile', 'student.department'])
return User::select(['id', 'username', 'email', 'is_active'])
->with([
'profile:id,user_id,full_name,phone_number,gender,birth_place,birth_date,address',
'student:id,user_id,student_number,department_id,enrollment_year,status',
'student.department:id,name',
])
->whereHas('roles', fn ($q) => $q->where('name', 'mahasiswa'))
->when($search, fn ($q) => $q->where(function ($query) use ($search) {
$query->where('username', 'like', "%{$search}%")

View File

@ -124,8 +124,8 @@ public function run(): void
],
],
[
'username' => 'komoala',
'email' => 'komoala@student.itmpwk.ac.id',
'username' => 'komala',
'email' => 'komala@student.itmpwk.ac.id',
'roles' => ['mahasiswa'],
'profile' => [
'full_name' => 'Komala Dewi',