CourseSearch.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Traits;
  3. trait CourseSearch{
  4. public static function searchByRequest($request) {
  5. $ids = [];
  6. $level_ids = [];
  7. $subject_ids = [];
  8. $institute_ids = [];
  9. $intake_ids = [];
  10. $length_ids = [];
  11. $requirement_ids = [];
  12. $fees_ids = [];
  13. $tags_ids = [];
  14. $query = model('Course')::where('destination_id', $request->destination_id);
  15. $all_ids = (clone $query)->pluck('id')->toArray();
  16. $search_type = $request->search_type;
  17. $search_text = $request->search_text;
  18. if($request->level_id){
  19. $level_ids = (clone $query)->where('level_id', $request->level_id)->pluck('id')->toArray();
  20. }else{
  21. $level_ids = $all_ids;
  22. }
  23. if($search_type == 'subject'){
  24. if($search_text != null){
  25. $subject_ids = (clone $query)->where('name', 'like', '%' . $search_text . '%')->pluck('id')->toArray();
  26. }else{
  27. $subject_ids = $all_ids;
  28. }
  29. }else{
  30. if($search_text != null){
  31. $subject_ids = (clone $query)->whereHas('institute', function($qry) use($search_text){
  32. $qry->where('name', 'like', '%' . $search_text . '%');
  33. })->pluck('id')->toArray();
  34. }else{
  35. $subject_ids = $all_ids;
  36. }
  37. }
  38. if($request->institute_ids){
  39. $institute_ids = (clone $query)->whereIn('institute_id', $request->institute_ids)->pluck('id')->toArray();
  40. }else{
  41. $institute_ids = $all_ids;
  42. }
  43. if($request->intake_ids){
  44. $intakeCheck = model('CourseIntake')::whereIn('Intake', $request->intake_ids)->pluck('course_id')->toArray();
  45. $intake_ids = (clone $query)->whereIn('id', $intakeCheck)->pluck('id')->toArray();
  46. }else{
  47. $intake_ids = $all_ids;
  48. }
  49. if($request->durations){
  50. $length_ids = (clone $query)->whereIn('length', $request->durations)->pluck('id')->toArray();
  51. }else{
  52. $length_ids = $all_ids;
  53. }
  54. if($request->course_fees){
  55. $range = [
  56. '15000' => [0,15000],
  57. '25000' => [15000,25000],
  58. '35000' => [25000,35000],
  59. '50000' => [35000,50000],
  60. '56000' => [50000,1000000]
  61. ];
  62. foreach($request->course_fees as $feeRange){
  63. $limit = $range[$feeRange];
  64. $from = $limit[0];
  65. $to = $limit[1];
  66. $range_ids = (clone $query)->where('tution_fee','>=', $from)->where('tution_fee','<=', $to)->pluck('id')->toArray();
  67. $fees_ids = array_merge($fees_ids, $range_ids);
  68. }
  69. }else{
  70. $fees_ids = $all_ids;
  71. }
  72. if($request->english_requirements){
  73. $requirement_ids = (clone $query)->whereNotNull($request->english_requirements)->pluck('id')->toArray();
  74. }else{
  75. $requirement_ids = $all_ids;
  76. }
  77. $ids = array_intersect($all_ids, $level_ids, $subject_ids, $institute_ids, $intake_ids, $length_ids, $requirement_ids, $fees_ids);
  78. if($search_text != null){
  79. $tags_ids = model('CourseTag')::whereIn('course_id', $level_ids)->where('keyword', 'like', '%' . $search_text . '%')->pluck('course_id')->toArray();
  80. $ids = array_merge($ids, $tags_ids);
  81. }
  82. return $ids;
  83. }
  84. }
  85. ?>