2016-10-17 46 views
1

我試圖在索引頁上顯示結果列表時實現displayPerPage(10,25,50等)。不過,我很難如何將它追加到URL而不替換其他查詢字符串。在Laravel中附加到url

實施例:

我有一個分頁程序然而,切換頁面如果我5頁例如,選擇每頁顯示在我的選擇下拉10分的結果,它會加載結果,但擦除頁= 5從網址。如何添加displayPerPage而不擦除當前位於頁面上的位置。

感謝您的任何幫助。

我知道它可能沒有創建自定義分頁程序,因爲我已經在另一個使用laravel 4的項目上完成了它,但是這是一段時間後,我似乎無法弄清楚如何再次執行它。

+0

如果您嘗試分頁,那麼您可以使用這個$ users = DB :: table('users') - > simplePaginate(15); – EaBangalore

+0

一個可以通過這個來查看 – EaBangalore

回答

0

假設$對象是你分頁項目的列表中,您認爲您可以添加分頁程序那樣:

{{$objects->appends(request()->query())->links()}} 

和查詢字符串像你requestPerPage將進行

+0

問題是當我發送$ displayPerPage到相同的網址,它不記得我的頁碼....我在PHP中寫了一個快速定製的url解析器函數,並解決了問題那樣。我會在下面發佈它,希望它能幫助某個人 – max234435

0

後爲了解決這個問題,我在php中編寫了一個url解析器幫助函數來查找並替換URL中的某些內容。符號(查詢部分)

//for displayPerPage, orderBy and sortBy parse the url and replace with new values 

function urlQueryParse($queryToReplace, $newValue){ 

// extract query from url 
$fullUrl = Request::fullUrl(); 

if(strpos($fullUrl, '?') !== false){ 

    //find everything after the ? in url 
    $url = substr($fullUrl, strpos($fullUrl, "?") + 1); 

//  check url to make sure $queryToReplace exists in the url 

if(strpos($url, $queryToReplace) !== false) { 

//  look through the remaining url query and replace whats given to the function 
     $newUrL = preg_replace('/('. $queryToReplace. '\=)([^&]+)/', $queryToReplace.'='.$newValue, $url); 

     return Request::url()."?{$newUrL}"; 
    } 

//  if the ? exists but the queryToReplace is not in the url, then add it to the query string 
    return Request::url()."?{$url}&{$queryToReplace}={$newValue}"; 

} 
//if the url doesnt have ? sign then simply insert the new value 
return Request::url()."?{$queryToReplace}={$newValue}"; 


} 

希望這可以幫助某人。如果您有任何改進的方法,歡迎提出建議。

P.S.我正在研究Laravel 5.2