You could drop the whereHas function from your query, since with will load the courses if they exist for the users that have some and simply return null for the user that don't.
Nov 21, 2022
3
Level 5
laravel wherehas is slow for more than 100k records
Hello All, I have a tables like below,
users
id, name, email
courses
id, course_name, description
and user_courses
id, user_id, course_id
now i am trying to fetch user with assigned courses so in user model i have added code like below,
public function assignedcourses()
{
return $this->belongsToMany('App\Course','user_courses','user_id','course_id');
}
now i am trying to fetch data like below,
$coursemanagers = User::whereHas('assignedcourses')->with('assignedcourses')->get();
But above code is taking more time like more than 3 minutes to display the data. how to optimise above query?
Thank you
Please or to participate in this conversation.