2016-10-14 176 views
1

我的分頁正確處理第一頁。它正確顯示2個名稱。當我想在接下來的頁面上看到其他結果時,它將不顯示任何與我當前的代碼。當我製作一個靜態數組時:搜索結果分頁laravel

$searchResults = [ 
      'item1', 
      'item2', 
      'item3', 
      'item4', 
      'item5', 
      ]; 

它會在不同的頁面上正確顯示數組。 當我從我的sql查詢中創建一個數組時,它在接下來的頁面上什麼也不顯示。 我使用var_dump,並在第二頁上,我沒有看到我創建的數組的任何東西。

這是我的控制器代碼:

public function zoekdocument(Request $request) //voor het zoeken van software 
{ 

$zoekdocument = \Request::get('zoekdocument'); 

$searchResult = array(); 

$searchdoc = \DB::table('visits')->where('title', 'LIKE', '%' . $zoekdocument . '%')->paginate(); 

foreach($searchdoc as $searchdocument){ 

    $filedir = $searchdocument->pagename; //wordt gedurende loop tot die finisht in de array gezet. 

    $searchResult[] = $filedir; //dat wordt hier gedaan. 
} 

    $currentPage = LengthAwarePaginator::resolveCurrentPage(); 

    $page = $request->has('page') ? $request->get('page') : 1; 

    //Create a new Laravel collection from the array data 
    $collection = new Collection($searchResult); 

    $perPage = 2; 

    $currentPageSearchResults = $collection-> slice (($currentPage -1) * $perPage, $perPage)->all(); 
    //Create our paginator and pass it to the view 
    $paginatedSearchResults= new 

LengthAwarePaginator($currentPageSearchResults, count($collection), $perPage, $page, ['path' => $request->url(),'query' => $request->query(), 

return view('Documentatie.result', ['results' => $paginatedSearchResults])->with('zoekdocument', $zoekdocument); 

這是我的看法代碼:

@section('content') 

<!-- Striped rows --> 
<div class="panel panel-flat"> 
    <div class="panel-heading"> 
    <h5 class="panel-title text-center">Documentatie overzicht resultaat</h5> 
    <div class="heading-elements"> 
    </div> 
    </div> 

    <div class="text-center"> 
    <h2>Zoekresultaat van: <b><i>{{$zoekdocument}}</b></i></h5> 
    </div> 

@foreach ($results as $result) 
     <p>{{ $result }}</p> 
    @endforeach 

    <?php echo $results->render(); ?> 




@endsection 

回答

1

我改變了我的代碼,paginate()get()並且它現在可以工作。

0

我認爲下面的代碼應該通過顯示分頁鏈接爲每laravel文檔修復錯誤:

{{ $users->links() }} 

代替

<?php echo $results->render(); ?> 

這裏是文檔鏈接:https://laravel.com/docs/5.3/pagination 讓我知道你的作品,如果沒有,我會盡力幫助找到解決辦法。

+0

它不起作用。它的奇怪,當我做一個靜態數組時,它的工作原理。我認爲這是我的SQL查詢出錯。我的查詢檢查與搜索到的單詞是否一致,並創建一個數組。例如:keyword =「te」,並且查詢在數據庫中搜索帶有「te」的名稱。之後,它將通過循環將其放入數組中。當我點擊下一頁時,數組是空的。 – jurh