feat: update StudentsExport and StudentService; inject User dependency and refine data retrieval methods

This commit is contained in:
Yoga Pangestu 2026-09-04 10:10:57 +07:00
parent 7ea4fb067d
commit ceeb1e325d
3 changed files with 14 additions and 6 deletions

View File

@ -18,6 +18,7 @@ class StudentsExport implements FromCollection, ShouldAutoSize, WithEvents, With
use StyledHeadingRow;
public function __construct(
private readonly User $user,
private readonly string $search = '',
private readonly ?string $gender = null,
private readonly ?int $departmentId = null,
@ -32,7 +33,7 @@ public function title(): string
public function collection(): Enumerable
{
return app(StudentService::class)->forExport($this->search, $this->gender, $this->departmentId, $this->status, $this->enrollmentYear);
return app(StudentService::class)->forExport($this->user, $this->search, $this->gender, $this->departmentId, $this->status, $this->enrollmentYear);
}
public function headings(): array

View File

@ -31,6 +31,7 @@ public function index(PaginatedRequest $request): Response
{
return Inertia::render('admin/users/students/index', [
'students' => $this->service->paginated(
$request->user(),
...$request->validatedWithDefaults(),
gender: $request->validated('gender'),
departmentId: $request->validated('department_id'),
@ -112,6 +113,7 @@ public function updateUserStatus(UpdateStatusRequest $request, User $user): Redi
public function export(PaginatedRequest $request): BinaryFileResponse
{
return Excel::download(new StudentsExport(
user: $request->user(),
search: $request->validated('search') ?? '',
gender: $request->validated('gender'),
departmentId: $request->validated('department_id'),

View File

@ -14,9 +14,9 @@
class StudentService
{
public function paginated(int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): LengthAwarePaginator
public function paginated(User $user, int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): LengthAwarePaginator
{
return $this->filteredQuery($search, $gender, $departmentId, $status, $enrollmentYear)
return $this->filteredQuery($user, $search, $gender, $departmentId, $status, $enrollmentYear)
->latest()
->paginate($perPage, ['id', 'username', 'email', 'is_active']);
}
@ -109,9 +109,9 @@ public function updateUserStatus(User $user, bool $isActive): void
$user->update(['is_active' => $isActive]);
}
public function forExport(string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): Collection
public function forExport(User $user, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): Collection
{
return $this->filteredQuery($search, $gender, $departmentId, $status, $enrollmentYear)
return $this->filteredQuery($user, $search, $gender, $departmentId, $status, $enrollmentYear)
->orderBy('created_at', 'desc')
->get(['id', 'username', 'email', 'is_active']);
}
@ -142,14 +142,19 @@ public function getEnrollmentYears(): array
->all();
}
private function filteredQuery(string $search, ?string $gender, ?int $departmentId, ?string $status, ?int $enrollmentYear = null): Builder
private function filteredQuery(User $user, string $search, ?string $gender, ?int $departmentId, ?string $status, ?int $enrollmentYear = null): Builder
{
$taughtDepartmentIds = $user->hasRole(UserRole::Dosen->value)
? $user->lecturer?->departments()->pluck('departments.id')->all() ?? []
: null;
return User::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,current_semester,status',
'student.department:id,name',
])
->whereHas('roles', fn ($q) => $q->where('name', UserRole::Mahasiswa->value))
->when($taughtDepartmentIds !== null, fn ($q) => $q->whereHas('student', fn ($q) => $q->whereIn('department_id', $taughtDepartmentIds)))
->when($search, fn ($q) => $q->where(function ($query) use ($search) {
$query->where('username', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%")