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.

105 lines
3.8 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. class SearchController extends Controller
  16. {
  17. public function search(Request $request){
  18. // validation
  19. $this->validate($request,[
  20. 'search' => 'required',
  21. 'table' => 'required',
  22. ]);
  23. $search = $request->input('search');
  24. $table = $request->input('table');
  25. switch($table) {
  26. case('summarie'):
  27. $results = Summarie::where('keywords', 'LIKE', '%'.$search.'%')->orWhere('type', 'LIKE', '%'.$search.'%')->sortable()->paginate(10);
  28. foreach ($results as $result) {
  29. $brewery = Brewerie::where('id', $result->brewery_id)->value('name');
  30. $result['brewery']=$brewery;
  31. $hops = Hopaddition::where('beer_id', $result->beer_id)->distinct('hop_id');
  32. $hoparr=array();
  33. $i = 0;
  34. foreach ($hops as $hop){
  35. $hopname = Hop::where('id', $hop->hop_id)->value('name');
  36. if ($i == 0){
  37. $hoparr[]= array('id'=>$hop->hop_id, 'name'=>$hopname);
  38. } else {
  39. array_push($hoparr, array('id'=>$hop->hop_id, 'name'=>$hopname));
  40. };
  41. $i++;
  42. };
  43. $result['hops']=$hoparr;
  44. $grains = Grainbill::where('beer_id', $result->beer_id)->get();
  45. $grainarr = array();
  46. $i = 0;
  47. foreach ($grains as $grain){
  48. $grainname = Grain::where('id', $grain->grain_id)->value('name');
  49. if ($i == 0){
  50. $grainarr[]= array('id'=>$grain->grain_id, 'name'=>$grainname, 'grain_bill'=>$grain->grain_bill);
  51. } else {
  52. array_push($grainarr, array('id'=>$grain->grain_id, 'name'=>$grainname, 'grain_bill'=>$grain->grain_bill));
  53. };
  54. $i++;
  55. };
  56. $result['grains'] = $grainarr;
  57. $yeasts = Fermentation::where('beer_id', $result->beer_id)->distinct('yeast_id');
  58. $yeastarr=array();
  59. $i = 0;
  60. foreach ($yeasts as $yeast){
  61. $yeastname = Yeast::where('id', $yeast->yeast_id)->value('name');
  62. if ($i == 0){
  63. $yeastarr[]= array('id'=>$yeast->yeast_id, 'name'=>$yeastname);
  64. } else {
  65. array_push($yeastarr, array('id'=>$yeast->yeast_id, 'name'=>$yeastnname));
  66. };
  67. $i++;
  68. };
  69. $result['yeasts']=$yeastarr;
  70. $adjuncts = Adjunctaddition::where('beer_id', $result->beer_id)->distinct('adjunct_id');
  71. $adjunctarr = array();
  72. $i = 0;
  73. foreach ($adjuncts as $adjunct){
  74. $adjunctname = Adjunct::where('id', $adjunct->adjunct_id)->value('name');
  75. if ($i == 0){
  76. $adjunctarr[]= array('id'=>$adjunct->adjunct_id, 'name'=>$adjunctname);
  77. } else {
  78. array_push($adjunctarr, array('id'=>$adjunct->adjunct_id, 'name'=>$adjunctnname));
  79. };
  80. $i++;
  81. };
  82. $result['adjuncts']=$adjunctarr;
  83. }
  84. break;
  85. case('brewerie'):
  86. break;
  87. case('hops'):
  88. break;
  89. case('yeast'):
  90. break;
  91. default:
  92. $msg = "You fucked up somehow!";
  93. }
  94. $data = array('results'=>$results, 'table'=>$table);
  95. return view('search.results')->with($data);
  96. }
  97. }