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.

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