Вы можете попробовать вот так:
$teachers = User::where(function($sub) use($name) {
$sub->where('first_name', $name)
->orWhereHas('teacherSubject', function($s) use($name) {
$s->where('subject', $name);
});
})whereHas('teacherSubject', function ($query) use($conditions){
$query->where($conditions);
})->with('teacherSubject')->get();
Здесь $name
- первое имя для поиска.