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.

92 lines
3.2 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. $result['hops']=[];
  33. $i = 0;
  34. foreach ($hops as $hop){
  35. $hopname = Hop::where('id', $hop->id)->value('name');
  36. $result['hops'][$i]['id'] = $hop->id;
  37. $result['hops'][$i]['name'] = $hopname;
  38. $i++;
  39. };
  40. $grains = Grainbill::where('beer_id', $result->beer_id)->get();
  41. $grainarr = array();
  42. foreach ($grains as $grain){
  43. $grainname = Grain::where('id', $grain->grain_id)->value('name');
  44. if ($i == 0){
  45. $grainarr[]= array('id'=>$grain->grain_id, 'name'=>$grainname);
  46. } else {
  47. array_push($grainarr, array('id'=>$grain->grain_id, 'name'=>$grainname));
  48. };
  49. $i++;
  50. };
  51. $result['grains'] = $grainarr;
  52. $yeasts = Fermentation::where('beer_id', $result->beer_id)->distinct('yeast_id');
  53. $result['yeasts']=[];
  54. $i = 0;
  55. foreach ($yeasts as $yeast){
  56. $yeastname = Yeast::where('id', $yeast->id)->value('name');
  57. $result['yeasts'][$i]['id'] = $yeast->id;
  58. $result['yeasts'][$i]['name'] = $yeastname;
  59. $i++;
  60. };
  61. $adjuncts = Adjunctaddition::where('beer_id', $result->beer_id)->distinct('adjunct_id');
  62. $result['adjuncts']=[];
  63. $i = 0;
  64. foreach ($adjuncts as $adjunct){
  65. $adjunctname = Adjunct::where('id', $adjunct->id)->value('name');
  66. $result['adjuncts'][$i]['id'] = $adjunct->id;
  67. $result['adjuncts'][$i]['name'] = $adjunctname;
  68. $i++;
  69. };
  70. }
  71. break;
  72. case('brewerie'):
  73. break;
  74. case('hops'):
  75. break;
  76. case('yeast'):
  77. break;
  78. default:
  79. $msg = "You fucked up somehow!";
  80. }
  81. $data = array('results'=>$results, 'table'=>$table);
  82. return view('search.results')->with($data);
  83. }
  84. }