$students = Student::with(["user.passport", "group.speciality"]);
if(\request("group_id")){ // если есть id группы
$students = $students->whereHas("group", function ($query) { $query->where("id", \request("group_id")); });
}
if(\request("speciality_id")){ // если есть id специальности
$students = $students->whereHas("group.speciality", function ($query) { $query->where("id", \request("speciality_id")); });
}
$students = $students->paginate(\request("page_size") ? : 10)->toArray();
return response()->json($students);