Level 70
@hosamalzagh I may think in a bit different way.
In your model, add a new method -
class Exam extends Model
{
public function students(){
return $this->belongsToMany('App\Student','exam_starts','exam_id','student_id')
->withPivot(['exam_class_id','Attend','pass','Status','exam_code_id'])->withTimestamps();
}
public function getUniqueStudents(){
return $this->students->where([
'exam_id' => 3,
'status' => 1
])
->groupBy('student_id')
->get();
}
}
BTW, you need to use groupBy('student_id') as you described in your question.
Then call it in the controller-
\App\exam->getUniqueStudents();