From 415f82d9a540e7853c9ce8e7a6fae0ef70547b27 Mon Sep 17 00:00:00 2001 From: jk Date: Thu, 24 Jun 2021 09:35:03 +0100 Subject: [PATCH] first iteration of search functionality - likely to fail hard --- app/Http/Controllers/SearchController.php | 76 ++++++++++++++++++++++- app/Models/Search.php | 6 +- resources/views/beers/index.blade.php | 1 + resources/views/search/index.blade.php | 19 ++++++ routes/web.php | 6 +- 5 files changed, 104 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index 4f3ba1f..3d809a0 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -3,8 +3,82 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use App\Models\Summarie; +use App\Models\Brewerie; +use App\Models\Hopaddition; +use App\Models\Hop; +use App\Models\Grain; +use App\Models\Yeast; +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('summaries'): + $results = Summarie::where('keywords', 'LIKE', '%'.$search.'%')->orWhere('type', 'LIKE', '%'.$search.'%')->get(); + 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)->distinct('grain_id'); + $result['grains']=[]; + $i = 0; + foreach ($grains as $grain){ + $grainname = Grain::where('id', $grain->id)->value('name'); + $result['grains'][$i]['id'] = $grain->id; + $result['grains'][$i]['name'] = $grainname; + $i++; + }; + $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'] = $hopname; + $i++; + }; + } + break; + case('breweries'): + + break; + case('hops'): + + break; + case('yeasts'): + + break; + default: + $msg = "You fucked up somehow!"; + $results = $results::sortable()->paginate(10); + return('search.results')->with($results); + } } diff --git a/app/Models/Search.php b/app/Models/Search.php index b8652b9..4dbd859 100644 --- a/app/Models/Search.php +++ b/app/Models/Search.php @@ -2,10 +2,12 @@ namespace App\Models; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Kyslik\ColumnSortable\Sortable; class Search extends Model { - use HasFactory; + use Sortable; + + public $sortable = ['name', 'keywords', 'brewery', 'hops', 'grains', 'yeasts', 'adjuncts']; } diff --git a/resources/views/beers/index.blade.php b/resources/views/beers/index.blade.php index 69a2262..98c0617 100644 --- a/resources/views/beers/index.blade.php +++ b/resources/views/beers/index.blade.php @@ -6,6 +6,7 @@
@include('inc.navbar') +@include('search.index')
Home -> {{ __('Beer List') }}Go Back
diff --git a/resources/views/search/index.blade.php b/resources/views/search/index.blade.php index e69de29..9551727 100644 --- a/resources/views/search/index.blade.php +++ b/resources/views/search/index.blade.php @@ -0,0 +1,19 @@ +
+ @csrf +
+ + +
+
+ Select table: + + + + + + + + +
+ +
diff --git a/routes/web.php b/routes/web.php index 1b134a7..40b068e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,7 +1,7 @@ name('yeast'); Route::get('/hop/{hopID}', [App\Http\Controllers\HopcardController::class, 'index'])->name('hop'); Route::get('/adjunct/{adjunctID}', [App\Http\Controllers\AdjunctcardController::class, 'index'])->name('adjunct'); +Route::get('search.results', function(){ + $searchresults = Search::sortable()->paginate(20); + return view('search.results', compact('searchresults')); +});