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.

88 lines
3.0 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Summarie;
  5. use App\Models\Brewerie;
  6. use App\Models\Hopaddition;
  7. use App\Models\Hop;
  8. use App\Models\Grainbill;
  9. use App\Models\Grain;
  10. use App\Models\Fermentation;
  11. use App\Models\Yeast;
  12. use App\Models\Adjunctaddition;
  13. use App\Models\Adjunct;
  14. class SearchController extends Controller
  15. {
  16. public function search(Request $request){
  17. // validation
  18. $this->validate($request,[
  19. 'search' => 'required',
  20. 'table' => 'required',
  21. ]);
  22. $search = $request->input('search');
  23. $table = $request->input('table');
  24. switch($table) {
  25. case('summarie'):
  26. $results = Summarie::where('keywords', 'LIKE', '%'.$search.'%')->orWhere('type', 'LIKE', '%'.$search.'%')->get();
  27. foreach ($results as $result) {
  28. $brewery = Brewerie::where('id', $result->brewery_id)->value('name');
  29. $result['brewery']=$brewery;
  30. $hops = Hopaddition::where('beer_id', $result->beer_id)->distinct('hop_id');
  31. $result['hops']=[];
  32. $i = 0;
  33. foreach ($hops as $hop){
  34. $hopname = Hop::where('id', $hop->id)->value('name');
  35. $result['hops'][$i]['id'] = $hop->id;
  36. $result['hops'][$i]['name'] = $hopname;
  37. $i++;
  38. };
  39. $grains = Grainbill::where('beer_id', $result->beer_id)->distinct('grain_id');
  40. $result['grains']=[];
  41. $i = 0;
  42. foreach ($grains as $grain){
  43. $grainname = Grain::where('id', $grain->id)->value('name');
  44. $result['grains'][$i]['id'] = $grain->id;
  45. $result['grains'][$i]['name'] = $grainname;
  46. $i++;
  47. };
  48. $yeasts = Fermentation::where('beer_id', $result->beer_id)->distinct('yeast_id');
  49. $result['yeasts']=[];
  50. $i = 0;
  51. foreach ($yeasts as $yeast){
  52. $yeastname = Yeast::where('id', $yeast->id)->value('name');
  53. $result['yeasts'][$i]['id'] = $yeast->id;
  54. $result['yeasts'][$i]['name'] = $yeastname;
  55. $i++;
  56. };
  57. $adjuncts = Adjunctaddition::where('beer_id', $result->beer_id)->distinct('adjunct_id');
  58. $result['adjuncts']=[];
  59. $i = 0;
  60. foreach ($adjuncts as $adjunct){
  61. $adjunctname = Adjunct::where('id', $adjunct->id)->value('name');
  62. $result['adjuncts'][$i]['id'] = $adjunct->id;
  63. $result['adjuncts'][$i]['name'] = $hopname;
  64. $i++;
  65. };
  66. }
  67. break;
  68. case('brewerie'):
  69. break;
  70. case('hops'):
  71. break;
  72. case('yeast'):
  73. break;
  74. default:
  75. $msg = "You fucked up somehow!";
  76. }
  77. $results = $results::sortable()->paginate(10);
  78. return('search.results')->with($results);
  79. }
  80. }