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.
 
 
 
 
 
 

92 lines
3.2 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;
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');
$result['hops']=[];
$i = 0;
foreach ($hops as $hop){
$hopname = Hop::where('id', $hop->id)->value('name');
$result['hops'][$i]['id'] = $hop->id;
$result['hops'][$i]['name'] = $hopname;
$i++;
};
$grains = Grainbill::where('beer_id', $result->beer_id)->get();
$grainarr = array();
foreach ($grains as $grain){
$grainname = Grain::where('id', $grain->grain_id)->value('name');
if ($i == 0){
$grainarr[]= array('id'=>$grain->grain_id, 'name'=>$grainname);
} else {
array_push($grainarr, array('id'=>$grain->grain_id, 'name'=>$grainname));
};
$i++;
};
$result['grains'] = $grainarr;
$yeasts = Fermentation::where('beer_id', $result->beer_id)->distinct('yeast_id');
$result['yeasts']=[];
$i = 0;
foreach ($yeasts as $yeast){
$yeastname = Yeast::where('id', $yeast->id)->value('name');
$result['yeasts'][$i]['id'] = $yeast->id;
$result['yeasts'][$i]['name'] = $yeastname;
$i++;
};
$adjuncts = Adjunctaddition::where('beer_id', $result->beer_id)->distinct('adjunct_id');
$result['adjuncts']=[];
$i = 0;
foreach ($adjuncts as $adjunct){
$adjunctname = Adjunct::where('id', $adjunct->id)->value('name');
$result['adjuncts'][$i]['id'] = $adjunct->id;
$result['adjuncts'][$i]['name'] = $adjunctname;
$i++;
};
}
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);
}
}