From c6b3b7b7f0e1d5f5693297f5a180333d4fcd1721 Mon Sep 17 00:00:00 2001 From: Kovah Date: Wed, 23 Jan 2019 22:02:02 +0100 Subject: [PATCH] Allow advanced searching by tags --- app/Http/Controllers/App/SearchController.php | 48 +++++++++---------- app/Http/Requests/SearchRequest.php | 4 +- resources/lang/en/search.php | 2 +- .../actions/search/partials/tags-js.blade.php | 24 ++++++++++ .../views/actions/search/search.blade.php | 35 +++++++------- 5 files changed, 65 insertions(+), 48 deletions(-) create mode 100644 resources/views/actions/search/partials/tags-js.blade.php diff --git a/app/Http/Controllers/App/SearchController.php b/app/Http/Controllers/App/SearchController.php index e950b108..bf650a3d 100644 --- a/app/Http/Controllers/App/SearchController.php +++ b/app/Http/Controllers/App/SearchController.php @@ -39,14 +39,11 @@ class SearchController extends Controller 'search_description' => false, 'private_only' => false, 'only_category' => 0, - 'only_tag' => 0, + 'only_tags' => '', 'order_by' => $this->order_by_options[0], ]) ->with('categories', Category::byUser(auth()->user()->id) - ->orderBy('name', 'asc')->get()) - ->with('tags', Tag::byUser(auth()->user()->id) - ->orderBy('name', 'asc')->get() - ); + ->orderBy('name', 'asc')->get()); } /** @@ -55,22 +52,26 @@ class SearchController extends Controller */ public function doSearch(SearchRequest $request) { - // Get the query - $raw_query = $request->get('query'); - $query = '%' . $raw_query . '%'; + $search_title = false; + $search_description = false; // Start building the search - $search = Link::byUser(auth()->id()) - ->where('url', 'like', $query); + $search = Link::byUser(auth()->id()); - // Also search for the title if applicable - if ($search_title = $request->get('search_title', false)) { - $search->orWhere('title', 'like', $query); - } + // Search for the URL + if ($raw_query = $request->get('query', false)) { + $query = '%' . $raw_query . '%'; + $search->where('url', 'like', $query); - // Also search for the title if applicable - if ($search_description = $request->get('search_description', false)) { - $search->orWhere('description', 'like', $query); + // Also search for the title if applicable + if ($search_title = $request->get('search_title', false)) { + $search->orWhere('title', 'like', $query); + } + + // Also search for the title if applicable + if ($search_description = $request->get('search_description', false)) { + $search->orWhere('description', 'like', $query); + } } // Show private only if applicable @@ -86,9 +87,9 @@ class SearchController extends Controller } // Show by specific tag only if applicable - if ($tag_id = $request->get('only_tag', false)) { - $search->whereHas('tags', function ($query) use ($tag_id) { - $query->where('id', $tag_id); + if ($tag_names = $request->get('only_tags', false)) { + $search->whereHas('tags', function ($query) use ($tag_names) { + $query->whereIn('name', explode(',', $tag_names)); }); } @@ -110,13 +111,10 @@ class SearchController extends Controller 'search_description' => $search_description, 'private_only' => $private_only, 'only_category' => $category_id, - 'only_tag' => $tag_id, + 'only_tags' => $tag_names, 'order_by' => $orderby, ]) ->with('categories', Category::byUser(auth()->user()->id) - ->orderBy('name', 'asc')->get()) - ->with('tags', Tag::byUser(auth()->user()->id) - ->orderBy('name', 'asc')->get() - ); + ->orderBy('name', 'asc')->get()); } } diff --git a/app/Http/Requests/SearchRequest.php b/app/Http/Requests/SearchRequest.php index 703ebc32..2ec31b58 100644 --- a/app/Http/Requests/SearchRequest.php +++ b/app/Http/Requests/SearchRequest.php @@ -23,8 +23,6 @@ class SearchRequest extends FormRequest */ public function rules() { - return [ - 'query' => 'required', - ]; + return []; } } diff --git a/resources/lang/en/search.php b/resources/lang/en/search.php index 1bfd480a..c1411a81 100644 --- a/resources/lang/en/search.php +++ b/resources/lang/en/search.php @@ -4,7 +4,7 @@ return [ 'results' => 'results', 'filter_by_category' => 'Filter by Category', - 'filter_by_tag' => 'Filter by Tag', + 'filter_by_tag' => 'Filter by Tag(s)', 'query' => 'Search for...', 'search_title' => 'Search Title', 'search_description' => 'Search Description', diff --git a/resources/views/actions/search/partials/tags-js.blade.php b/resources/views/actions/search/partials/tags-js.blade.php new file mode 100644 index 00000000..6aab1aff --- /dev/null +++ b/resources/views/actions/search/partials/tags-js.blade.php @@ -0,0 +1,24 @@ + diff --git a/resources/views/actions/search/search.blade.php b/resources/views/actions/search/search.blade.php index 3045f59b..35811d46 100644 --- a/resources/views/actions/search/search.blade.php +++ b/resources/views/actions/search/search.blade.php @@ -13,7 +13,7 @@
- @@ -27,7 +27,7 @@
-
+
@@ -37,7 +37,7 @@
-
+
-
+
@@ -60,11 +60,11 @@
-
+
- @foreach($categories as $category)
-
-