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.

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