My beer compendium
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
5.4 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Search;
  5. use App\Models\Summarie;
  6. use App\Models\Brewerie;
  7. use App\Models\Hopaddition;
  8. use App\Models\Hop;
  9. use App\Models\Grainbill;
  10. use App\Models\Grain;
  11. use App\Models\Fermentation;
  12. use App\Models\Yeast;
  13. use App\Models\Adjunctaddition;
  14. use App\Models\Adjunct;
  15. use App\Models\Mashe;
  16. class SearchController extends Controller
  17. {
  18. public function search(Request $request){
  19. // validation
  20. $this->validate($request,[
  21. 'search' => 'required',
  22. 'table' => 'required',
  23. ]);
  24. $search = $request->input('search');
  25. $table = $request->input('table');
  26. switch($table) {
  27. case('summarie'):
  28. $results = Summarie::where('keywords', 'LIKE', '%'.$search.'%')->orWhere('type', 'LIKE', '%'.$search.'%')->sortable()->paginate(10);
  29. foreach ($results as $result) {
  30. $brewery = Brewerie::where('id', $result->brewery_id)->value('name');
  31. $result['brewery']=$brewery;
  32. $hops = Hopaddition::where('beer_id', $result->beer_id)->distinct('hop_id')->get();
  33. $hoparr=array();
  34. $i = 0;
  35. foreach ($hops as $hop){
  36. $hopname = Hop::where('id', $hop->hop_id)->value('name');
  37. if ($i == 0){
  38. $hoparr[]= array('id'=>$hop->hop_id, 'name'=>$hopname);
  39. } else {
  40. array_push($hoparr, array('id'=>$hop->hop_id, 'name'=>$hopname));
  41. };
  42. $i++;
  43. };
  44. $result['hops']=$hoparr;
  45. $grains = Grainbill::where('beer_id', $result->beer_id)->get();
  46. $grainarr = array();
  47. $i = 0;
  48. foreach ($grains as $grain){
  49. $grainname = Grain::where('id', $grain->grain_id)->value('name');
  50. if ($i == 0){
  51. $grainarr[]= array('id'=>$grain->grain_id, 'name'=>$grainname, 'grain_bill'=>$grain->grain_bill);
  52. } else {
  53. array_push($grainarr, array('id'=>$grain->grain_id, 'name'=>$grainname, 'grain_bill'=>$grain->grain_bill));
  54. };
  55. $i++;
  56. };
  57. $result['grains'] = $grainarr;
  58. $yeasts = Fermentation::where('beer_id', $result->beer_id)->distinct('yeast_id')->get();
  59. $yeastarr=array();
  60. $i = 0;
  61. foreach ($yeasts as $yeast){
  62. $yeastname = Yeast::where('id', $yeast->yeast_id)->value('name');
  63. if ($i == 0){
  64. $yeastarr[]= array('id'=>$yeast->yeast_id, 'name'=>$yeastname);
  65. } else {
  66. array_push($yeastarr, array('id'=>$yeast->yeast_id, 'name'=>$yeastnname));
  67. };
  68. $i++;
  69. };
  70. $result['yeasts']=$yeastarr;
  71. $adjuncts = Adjunctaddition::where('beer_id', $result->beer_id)->distinct('adjunct_id')->get();
  72. $adjunctarr = array();
  73. $i = 0;
  74. foreach ($adjuncts as $adjunct){
  75. $adjunctname = Adjunct::where('id', $adjunct->adjunct_id)->value('name');
  76. if ($i == 0){
  77. $adjunctarr[]= array('id'=>$adjunct->adjunct_id, 'name'=>$adjunctname);
  78. } else {
  79. array_push($adjunctarr, array('id'=>$adjunct->adjunct_id, 'name'=>$adjunctname));
  80. };
  81. $i++;
  82. };
  83. $result['adjuncts']=$adjunctarr;
  84. $mashes = Mashe::where('beer_id', $result->beer_id)->get();
  85. $masharr = array();
  86. $i = 0;
  87. foreach ($mashes as $mash){
  88. if ($mash->stage == 1) {
  89. $result['boil'] = $mash->boil;
  90. }
  91. if ($i == 0){
  92. $masharr[] = array('id'=>$mash->id, 'stage'=>$mash->stage, 'duration'=>$mash->duration, 'temp'=>$mash->temp);
  93. } else {
  94. array_push($masharr, array('id'=>$mash->id, 'stage'=>$mash->stage, 'duration'=>$mash->duration, 'temp'=>$mash->temp));
  95. };
  96. $i++;
  97. };
  98. $result['mashes']=$masharr;
  99. $hopadditions = Hopaddition::where('beer_id', $result->beer_id)->distinct('timing')->get();
  100. $result['hopadditions'] = $hopadditions;
  101. $fermentations = Fermentation::where('beer_id', $result->beer_id)-get();
  102. $fermentarr = array();
  103. $i = 0;
  104. foreach ($fermentations as $fermentation){
  105. if ($i == 0){
  106. $fermentarr = array('stage'=>($i + 1), 'temp'=>$fermentation->temperature, 'duration'=>$fermentation->duration);
  107. } else {
  108. array_push($fermentarr, array('stage'=>($i + 1), 'temp'=>$fermentation->temperature, 'duration'=>$fermentation->duration));
  109. };
  110. $i++;
  111. };
  112. $result['fermentations'] = $fermentarr;
  113. }
  114. break;
  115. case('brewerie'):
  116. break;
  117. case('hops'):
  118. break;
  119. case('yeast'):
  120. break;
  121. default:
  122. $msg = "You fucked up somehow!";
  123. }
  124. $data = array('results'=>$results, 'table'=>$table);
  125. return view('search.results')->with($data);
  126. }
  127. }