tyo
tyo

Reputation: 83

how get search result to non space item

i have problem with search code on my web.lets say i have only one article with name cool car if i typing it some text bas on it on search bar it will be like this

cool -> it get result
car -> it get result
cool car -> it get result
(empty text) -> it get result with (isset($message)) 'found nothing'
xyz -> it get result with (isset($message)) 'found nothing'
coolcar -> page just go full blank without (isset($message)) or result even toolbar not show up  <this is the problem

this is my controller code

public function search(Request $request)
{
    $tags = tag::all();
    $artikelupdate = artikel::latest()->limit(3)->get();
    $categori = categori::all();
    $artikelterkait = artikel::orderByViews()->limit(3)->get();
    $message = "Sorry, but nothing matched your search terms. Please try again with some different keywords.";
    $q = Request::input('q');
    if($q != ""){
    $artikelall = artikel::orWhere('judul','LIKE', '%' . $q . '%')->paginate(5);
    $artikelall->appends(['q' => $q]);
    
    if(count($artikelall) > 0)
    return view('search')->withArtikelall($artikelall)->withArtikelterkait($artikelterkait)->withArtikelupdate($artikelupdate)->withTags($tags)->withCategori($categori)->withQuery ( $q );  
    }else{
    return view('search')->withCategori($categori)->withArtikelterkait($artikelterkait)->withMessage($message)->withArtikelupdate($artikelupdate)->withTags($tags)->withQuery ( $q );
    }
    
}    

this is my view blade.php

                <form method="POST" action="{{ url('/search') }}" role="search">
        {{ csrf_field() }}
        <div class="input-group f-search">
        <input type="text" name="q" class="form-control" placeholder="Search Title Here">
        <div class="input-group-append">
        <button class="btn btn-secondary" type="submit">
         <i class="fa fa-search"></i>
        </button>
         </div>
          </div>
          </form>

i just want when i typing item without space like coolcar it will show imassage.ty for advance.

Upvotes: 0

Views: 14

Answers (1)

tyo
tyo

Reputation: 83

this is i found to fix this looks silly but at least this is work for me if someone have better solution rather than add ridiclious multiple if else statement feel free to answer or just mayble close this question.

public function search(Request $request)
{
    $tags = tag::all();
    $artikelupdate = artikel::latest()->limit(3)->get();
    $categori = categori::all();
    $artikelterkait = artikel::orderByViews()->limit(3)->get();
    $message = "Sorry, but nothing matched your search terms. Please try again with some different keywords.";
    $q = Request::input('q');
    $arrDelChars = [' ','\'','-','.','?','!'];
    if ( ctype_alpha( str_replace( $arrDelChars, '', $q ) ) === true || is_numeric($q)) {
        $artikelall = artikel::Where('judul','LIKE', "%{$q}%")->paginate(5);
        $artikelall->appends(['q' => $q]);
        if(count($artikelall) > 0){
        return view('search')->withArtikelall($artikelall)->withArtikelterkait($artikelterkait)->withArtikelupdate($artikelupdate)->withTags($tags)->withCategori($categori)->withQuery ( $q );
        }else{
            return view('search')->withCategori($categori)->withArtikelterkait($artikelterkait)->withMessage($message)->withArtikelupdate($artikelupdate)->withTags($tags)->withQuery ( $q );
        }
    } else {
        return view('search')->withCategori($categori)->withArtikelterkait($artikelterkait)->withMessage($message)->withArtikelupdate($artikelupdate)->withTags($tags)->withQuery ( $q );
    }
    
}    

}

Upvotes: 1

Related Questions