searchlaravel-5.7laravel-pagination

Laravel Searching with Pagination?


I've been trying so hard to get this but I couldn't. Searching is all working fine also pagination is appearing, but whenever I click on any page number is opens a blank page since there is no route to "GET" that request, can anyone please lead to some place where I can find how to show other pages with a searching query or do I have to create get route to show other pages? I'm all confused what to do?

Tried With GET method Still, not working

Route

Route::get('search/','Coins@search')->name('coin.search');

Controller

public function search(){
        $keyword = Input::get('search');
        if($keyword){
            $result = [];
            $resultByName = Coin::
                                where('type','=','coin')
                                ->where('parent_id','=',0)
                                ->where('coin_name','LIKE',"%{$keyword}%")
                                ->orderBy('id', 'asc')
                                ->paginate(10);
            $resultByDescription = Coin::where('type','=','coin')
                ->where('parent_id','=',0)
                ->where('description','LIKE',"%{$keyword}%")
                ->orderBy('id', 'asc')
                ->paginate(10);
            if($resultByName->count()) {
                $result =  $resultByName;
                $message = "Coin Detail found in coin name !! based on searching data : ".$keyword;
            }else{
                if($resultByDescription->count()){
                    $result =  $resultByDescription;
                    $message = "Coin Detail found in description !! based on searching data : ".$keyword;
                }else{
                    $result = Coin::where('type','=','coin')->where('parent_id','=',0)->get();
                    $message = "No Coin Found !! based on searching data : ".$keyword;
                }
            }
            return view($this->layout.'coins')->with([
                'allCoins' => $result,
                'message'  => $message
            ]);
        }
    }

Blade

  {!! Form::open([
                    'route'   => 'coin.search'
                    ,'method' => 'GET'
                    ,'id'     => 'coinSearch'])
   !!}
         <input type="text" placeholder="Search Your Coin Here . . . " name="search">
         <button type="submit" class="btn btn-sm btn-primary">Search</button>
  {!! Form::close() !!}
{!! $allCoins->render() !!}

Solution

  • Call the appends method on the result object returned by the paginate query. It accepts an array as a parameter and in the array, you can specify as many variables as you want.

      $results->appends ();
    

    Such as one of your query will become

    $resultByName = Coin::where('type','=','coin')
                     ->where('parent_id','=',0)
                     ->where('coin_name','LIKE',"%{$keyword}%")
                     ->orderBy('id', 'asc')
                     ->paginate(10);
     $resultByName->appends (array ('keyword' => $keyword));
    

    It will append all the variables in the link and your URL will become

    http://your_domain/search?keyword=$keyword(value of this variable)
    

    Pagination will work without adding any custom code.