diff options
Diffstat (limited to 'app/Http/Controllers/CocktailController.php')
| -rw-r--r-- | app/Http/Controllers/CocktailController.php | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/app/Http/Controllers/CocktailController.php b/app/Http/Controllers/CocktailController.php index 21655d2..ef24ab8 100644 --- a/app/Http/Controllers/CocktailController.php +++ b/app/Http/Controllers/CocktailController.php @@ -28,17 +28,40 @@ class CocktailController extends Controller public function search(Request $request) { $ingredients = $request->input("i"); + if ( ! isset($ingredients) || empty($ingredients) ) { + $ingredients = array( $request->input("q") ); + } + $excludes = $request->input("i_ex"); + if ( ! isset($excludes) || empty($excludes) ) { + $excludes = array(); + } $cocktails = new Cocktail; foreach($ingredients as $q) { $q = Helper::escapeLike($q); $q = "%".$q."%"; - $cocktails = $cocktails->where(function($query) use ($q){ - $query->whereHas("getIngredients", function ($query) use ($q){ - $query->where('name', 'like', $q); + + if ( ! isset($excludes) || empty($excludes) ) { + $cocktails = $cocktails->where(function($query) use ($q){ + $query->whereHas("getIngredients", function ($query) use ($q){ + $query->where('name', 'like', $q); + }); }); - }); + } else { + foreach($excludes as $ex) { + $ex = Helper::escapeLike($ex); + $ex = "%".$ex."%"; + + $cocktails = $cocktails->where(function($query) use ($q, $ex){ + $query->whereHas("getIngredients", function ($query) use ($q){ + $query->where('name', 'like', $q); + })->whereDoesntHave("getIngredients", function ($query) use ($ex){ + $query->where('name', 'like', $ex); + }); + }); + } + } } $count = $cocktails->count(); @@ -51,6 +74,20 @@ class CocktailController extends Controller } $search_phrase = rtrim($search_phrase, " und "); - return view('cocktail_list', [ "cocktails" => $cocktails, "count" => $count, "search_terms" => array_filter($ingredients), "search_phrase" => $search_phrase ]); + $exclude_phrase = ""; + if ( isset($excludes) ) { + foreach(array_filter($excludes) as $term) { + if ( $exclude_phrase != "" ) + $exclude_phrase = $exclude_phrase . " und "; + $exclude_phrase = $exclude_phrase . ucwords($term); + } + $exclude_phrase = rtrim($exclude_phrase, " und "); + } + + $search_phrase .= " und nicht nach " . $exclude_phrase; + + + + return view('cocktail_list', [ "cocktails" => $cocktails, "count" => $count, "search_terms" => array_filter($ingredients), "search_phrase" => $search_phrase, "exclude_terms" => array_filter($excludes) ]); } } |
