ethar
4 weeks ago

Simplify Eloquent Code

Posted 4 weeks ago by ethar

in StudentCourse Model table (Pivot Table) i store('active_courses_id','student_id'),

In ActiveCourse i store (id,course_id)

in below query, I want to get all courses not registred to the user ($id),

I use two query, first to get activeCourse that user enrolls on it, then other query go get courses user did not enroll on it depend on the first query.

$userCourse=StudentCourseModel::select('active_courses_id')
   ->where('student_id',$id)
   ->get();
$courses = ActiveCourse::select('id', 'course_id')
   ->with('course:id,title_ar')
   ->where('active', 1)
   ->whereNotIn('id',$userCourse)
   ->get();

i want to ask if any way to use one query instead of two query

Please sign in or create an account to participate in this conversation.