role == 'admin') { return $next($request); } $watchHistory = $request->route('watch_history'); if (!($watchHistory instanceof WatchHistory)) { $watchHistory = WatchHistory::find($watchHistory); } if (!$watchHistory) { return back()->with('error', 'Invalid watch history'); } $course = Course::find($watchHistory->course_id); if (!$course) { return back()->with('error', 'Invalid course'); } if ($user->role == 'instructor' && $user->instructor_id == $course->instructor_id) { return $next($request); } $enrollment = CourseEnrollment::where('user_id', $user->id) ->where('course_id', $watchHistory->course_id) ->first(); if ($enrollment) { return $next($request); } return back()->with('error', 'You are not enrolled in this course'); } }