laravel創建分頁

在學習到laravel框架創建分頁時出現了運行錯誤。php

控制器ArticleController的代碼html

public function index()
    {
        $data=Article::orderBy('art_id','desc')->Paginate(2);
        return view('admin.article.index',compact('data'));

    }

在視圖index.blade.php中的代碼laravel

<div class="page_list">
    {{$data->links()}}
</div>

運行報錯瀏覽器

Method links does not exist. (View: D:\AppServ\www\mylaravel\resources\views\admin\article\index.blade.php)

參考http://stackoverflow.com/ques...app

Method links does not exist. (View: C:\xampp\htdocs\app\resources\views\search.blade.php)

CONTROLLER框架

$posts = Post::where('visible', 1)
->where('expire_date', '>', $current)->where('delete', 0);
$posts->paginate(1);
$posts = $posts->get();
return view('search', compact('posts'));

VIEWpost

<div class="pagination-bar text-center">
       {{ $posts->links() }}
</div>

Answer學習

Change you code to this:this

$posts = Post::where('visible', 1)
             ->where('expire_date', '>', $current)
             ->where('delete', 0)
             ->paginate(1);

return view('search', compact('posts'));

Your code doesn't work because you do not save paginate() results to a variable, like $posts = $posts->paginate(1);. Also, you shouldn't use get() or all() after paginate().url

看的似懂非懂的,實際上是不懂的!


又查看laravel技術文檔 http://laravelacademy.org/pos...

嘗試該換simplePaginate方法來實現,結果仍是不行!
其實用dd($data);向瀏覽器輸出時,能夠提取出頁碼元素。控制器部分應該時沒有問題的。只是抱着一絲但願試試,失敗!

又將index.blade.php中的改成

<div class="page_list">
   {!! $data->render() !!}
</div>

運行時經過,不過僅僅出現了

clipboard.png

覺得這樣可能時bug致使。就沒有繼續修改,畢竟深層原理不知道。

更重要的是,在技術文檔中三、在視圖中顯示分頁結果中闡述simplePaginate與paginate時,沒有在該部分明確說明其中的差異。

繼續看[ Laravel 5.1 文檔 ] 服務 —— 分頁部分,發如今3.3 更多幫助方法中有

  • $results->count()

  • $results->currentPage()

  • $results->hasMorePages()

  • $results->lastPage() (使用simplePaginate時無效)

  • $results->nextPageUrl()

  • $results->perPage()

  • $results->total() (使用simplePaginate時無效)

  • $results->url($page)

使用simplepaginate無效的方法,遂就查看ArticleController中時paginete仍是simplePaginate;發現時simplePaginate,將其改成paginate。再次運行。達到效果!

clipboard.png

具體緣由未知,待繼續深刻學習後爬代碼看結果。

相關文章
相關標籤/搜索