-
app/Policies/TeacherPolicy.php
Open in GitHubuse App\Enums\RoleEnum; use App\Models\Teacher; use App\Models\User; use Illuminate\Auth\Access\HandlesAuthorization; use Illuminate\Auth\Access\Response; class TeacherPolicy { use HandlesAuthorization; // public function viewTeacher(User $user, Teacher $teacher) { if ($user->hasRole(RoleEnum::TEACHER)){ return Response::allow(); } if ($user->id === $teacher->user_id){ Response::allow(); } return Response::deny(); } // }
-
app/Http/Controllers/Teacher/TeacherController.php
Open in GitHubuse App\Http\Controllers\RespondsWithHttpStatusController; use App\Models\Teacher; use App\Http\Resources\ClassroomResource; use App\Http\Resources\StudentResource; use App\Http\Resources\SubjectResource; class TeacherController extends RespondsWithHttpStatusController { public function index(Request $request) { $teacher = Teacher::query() ->where('user_id', auth()->id()) ->firstOrFail(); $classrooms = $teacher->whereRelation('classrooms', 'teacher_id', '=', $teacher->id)->get(); $students = $teacher->whereRelation('students', 'teacher_id', '=', $teacher->id)->get(); $subjects = $teacher->whereRelation('subjects', 'teacher_id', '=', $teacher->id)->get(); $this->authorize('viewTeacher', $teacher); return $this->respond([ 'students' => StudentResource::collection($students), 'subjects' => SubjectResource::collection($subjects), 'classrooms' => ClassroomResource::collection($classrooms), 'studentCount' => $students->count(), 'subjectCount' => $subjects->count(), 'classroomCount' => $classrooms->count() ]); } // }