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.

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