-1

Issue:

When I search in my posts I get result from posts and products both, but when I search in products I only get results from products (which is correct)

codes

my controller:

public function search(Request $request) {
        $search = request('search');
        $searchType = request('searchType');

        if(strcmp($searchType, "posts") == 0){
          $posts = Post::where('title', 'like', "%{$search}%")
          ->orWhere('description', 'like', "%{$search}%")
          ->get();
        }else{
          $products = Product::where('title', 'like', "%{$search}%")
          ->orWhere('description', 'like', "%{$search}%")
          ->get();
        }

        return view('front.search', compact('posts', 'products'));
    }

my blade (form)

<form class="search" action="/search" method="GET" role="search">
                        <input type="text" name="search" class="awesomplete search_box" list="mylist" placeholder="Enter your keyword ...">
                        <datalist id="mylist">
                          @foreach($products as $product)
                            <option>{{$product->title}}</option>
                          @endforeach
                        </datalist>
                        <div class="collections-selector">
                          <select class="single-option-selector" data-option="collection-option" id="collection-option" name="searchType">
                            <option value="products">Products</option>
                            <option value="posts">Posts</option>
                          </select>
                        </div>
                        <button class="search_submit" type="submit">
                          <svg aria-hidden="true" role="presentation" class="icon icon-search" viewBox="0 0 37 40"><path d="M35.6 36l-9.8-9.8c4.1-5.4 3.6-13.2-1.3-18.1-5.4-5.4-14.2-5.4-19.7 0-5.4 5.4-5.4 14.2 0 19.7 2.6 2.6 6.1 4.1 9.8 4.1 3 0 5.9-1 8.3-2.8l9.8 9.8c.4.4.9.6 1.4.6s1-.2 1.4-.6c.9-.9.9-2.1.1-2.9zm-20.9-8.2c-2.6 0-5.1-1-7-2.9-3.9-3.9-3.9-10.1 0-14C9.6 9 12.2 8 14.7 8s5.1 1 7 2.9c3.9 3.9 3.9 10.1 0 14-1.9 1.9-4.4 2.9-7 2.9z"></path></svg>
                        </button>
                      </form>

my route:

Route::any('/search', 'frontend\SearchController@search')->name('search');

my search result page:

@if(isset($posts))
 @forelse($posts as $post)
  {{$post->title}} <br>
 @empty
  no post!
 @endforelse
@endif


@if(isset($products))
 @forelse($products as $product)
  {{$product->title}} <br>
 @empty
  no product!
 @endforelse
@endif

screenshot

res1

PS: this screenshot was taken in posts search.

Additional

dd($posts);

res2

dd($products);

res3

In dd everything seems fine but in actual view they both come together. Any idea?

mafortis
  • 6,750
  • 23
  • 130
  • 288
  • How many blades template involved in this search? From the only clue you gave here, it seems like you have only 1 blade template, showing both the search form and the search result, which when you search posts, the products is printed from `...` section, confused you that it's returned from the search result but it's not? – Lionel Chan Feb 05 '18 at 03:47
  • @LionelChan there is only 1 blade involved to show results (form is in header-shared for all pages). and that divide result base on `@isset` . and to answer you other question: honestly i have no idea where `products` results coming from all i know is that `dd` result are OK! but in blade not. any idea? – mafortis Feb 05 '18 at 03:56
  • There is one section in your blade, `...`. I suspect it's from there. Can you comment out this datalist chunk, and do a search on posts again? I bet the products will be removed. OR, because you need to populate and you set the `products` variable, causing the search result to confuse with it – Lionel Chan Feb 05 '18 at 03:58
  • there is no `datalist` i cleaned all my blade and just blade codes that i provided in my question are in it. any other thought? – mafortis Feb 05 '18 at 04:02
  • I was saying this chunk https://i.imgur.com/rbmtmWt.png. You probably need to populate this section `$products` and this variable contaminated your search result. Try different variable name and the problem should be gone. – Lionel Chan Feb 05 '18 at 04:05
  • how to do that? – mafortis Feb 05 '18 at 04:07

1 Answers1

2

As of discussion, you set the $products variable in the header search form, and it contaminated your search result page when it's checking if $products is set, and it's indeed set so it is printed out (i.e, your search result is printing the products from the variable from your header-shared form).

Simply use a different variable name for the form in the shared blade template, like, instead of using $products, use a more specific $searchProducts.

Lionel Chan
  • 7,894
  • 5
  • 40
  • 69