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.
 
 
 
 
 
 

134 lines
5.4 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Search;
use App\Models\Summarie;
use App\Models\Brewerie;
use App\Models\Hopaddition;
use App\Models\Hop;
use App\Models\Grainbill;
use App\Models\Grain;
use App\Models\Fermentation;
use App\Models\Yeast;
use App\Models\Adjunctaddition;
use App\Models\Adjunct;
use App\Models\Mashe;
class SearchController extends Controller
{
public function search(Request $request){
// validation
$this->validate($request,[
'search' => 'required',
'table' => 'required',
]);
$search = $request->input('search');
$table = $request->input('table');
switch($table) {
case('summarie'):
$results = Summarie::where('keywords', 'LIKE', '%'.$search.'%')->orWhere('type', 'LIKE', '%'.$search.'%')->sortable()->paginate(10);
foreach ($results as $result) {
$brewery = Brewerie::where('id', $result->brewery_id)->value('name');
$result['brewery']=$brewery;
$hops = Hopaddition::where('beer_id', $result->beer_id)->distinct('hop_id')->get();
$hoparr=array();
$i = 0;
foreach ($hops as $hop){
$hopname = Hop::where('id', $hop->hop_id)->value('name');
if ($i == 0){
$hoparr[]= array('id'=>$hop->hop_id, 'name'=>$hopname);
} else {
array_push($hoparr, array('id'=>$hop->hop_id, 'name'=>$hopname));
};
$i++;
};
$result['hops']=$hoparr;
$grains = Grainbill::where('beer_id', $result->beer_id)->get();
$grainarr = array();
$i = 0;
foreach ($grains as $grain){
$grainname = Grain::where('id', $grain->grain_id)->value('name');
if ($i == 0){
$grainarr[]= array('id'=>$grain->grain_id, 'name'=>$grainname, 'grain_bill'=>$grain->grain_bill);
} else {
array_push($grainarr, array('id'=>$grain->grain_id, 'name'=>$grainname, 'grain_bill'=>$grain->grain_bill));
};
$i++;
};
$result['grains'] = $grainarr;
$yeasts = Fermentation::where('beer_id', $result->beer_id)->distinct('yeast_id')->get();
$yeastarr=array();
$i = 0;
foreach ($yeasts as $yeast){
$yeastname = Yeast::where('id', $yeast->yeast_id)->value('name');
if ($i == 0){
$yeastarr[]= array('id'=>$yeast->yeast_id, 'name'=>$yeastname);
} else {
array_push($yeastarr, array('id'=>$yeast->yeast_id, 'name'=>$yeastnname));
};
$i++;
};
$result['yeasts']=$yeastarr;
$adjuncts = Adjunctaddition::where('beer_id', $result->beer_id)->distinct('adjunct_id')->get();
$adjunctarr = array();
$i = 0;
foreach ($adjuncts as $adjunct){
$adjunctname = Adjunct::where('id', $adjunct->adjunct_id)->value('name');
if ($i == 0){
$adjunctarr[]= array('id'=>$adjunct->adjunct_id, 'name'=>$adjunctname);
} else {
array_push($adjunctarr, array('id'=>$adjunct->adjunct_id, 'name'=>$adjunctname));
};
$i++;
};
$result['adjuncts']=$adjunctarr;
$mashes = Mashe::where('beer_id', $result->beer_id)->get();
$masharr = array();
$i = 0;
foreach ($mashes as $mash){
if ($mash->stage == 1) {
$result['boil'] = $mash->boil;
}
if ($i == 0){
$masharr[] = array('id'=>$mash->id, 'stage'=>$mash->stage, 'duration'=>$mash->duration, 'temp'=>$mash->temp);
} else {
array_push($masharr, array('id'=>$mash->id, 'stage'=>$mash->stage, 'duration'=>$mash->duration, 'temp'=>$mash->temp));
};
$i++;
};
$result['mashes']=$masharr;
$hopadditions = Hopaddition::where('beer_id', $result->beer_id)->distinct('timing')->get();
$result['hopadditions'] = $hopadditions;
$fermentations = Fermentation::where('beer_id', $result->beer_id)-get();
$fermentarr = array();
$i = 0;
foreach ($fermentations as $fermentation){
if ($i == 0){
$fermentarr = array('stage'=>($i + 1), 'temp'=>$fermentation->temperature, 'duration'=>$fermentation->duration);
} else {
array_push($fermentarr, array('stage'=>($i + 1), 'temp'=>$fermentation->temperature, 'duration'=>$fermentation->duration));
};
$i++;
};
$result['fermentations'] = $fermentarr;
}
break;
case('brewerie'):
break;
case('hops'):
break;
case('yeast'):
break;
default:
$msg = "You fucked up somehow!";
}
$data = array('results'=>$results, 'table'=>$table);
return view('search.results')->with($data);
}
}