![](/img/trans.png)
[英]Call to a member function links() on array in laravel 8 with pagination
[英]Call to a member function links() on array in Laravel 5.6
我在Laravel 5.6中使用分頁來構建應用程序。 我正在使用Laravel DB而不是Eloquent。 但是,當我在視圖上添加分頁鏈接時,它在array上顯示對成員函數links()的調用錯誤。 這是因為我使用的是數組,默認值為帶有集合的對象。 我得到的輸出看起來像
array:12 [▼
"current_page" => 1
"data" => array:3 [▶]
"first_page_url" => "http://127.0.0.1:8000/posts?page=1"
"from" => 1
"last_page" => 3
"last_page_url" => "http://127.0.0.1:8000/posts?page=3"
"next_page_url" => "http://127.0.0.1:8000/posts?page=2"
"path" => "http://127.0.0.1:8000/posts"
"per_page" => 3
"prev_page_url" => null
"to" => 3
"total" => 9
]
當我使用常規分頁,即不轉換為數組時,輸出為
LengthAwarePaginator {#264 ▼
#total: 9
#lastPage: 3
#items: Collection {#251 ▶}
#perPage: 3
#currentPage: 1
#path: "http://127.0.0.1:8000/posts"
#query: []
#fragment: null
#pageName: "page"
}
我在這里面臨的問題是將其他數據添加到集合中
我不能使用pagination()方法的默認輸出返回值。 有人可以建議如何解決這個問題嗎?
使用下面的代碼,我得到所需的輸出。
步驟1:在文件中包含LengthAwarePaginator
use Illuminate\Pagination\LengthAwarePaginator;
步驟2:創建一個LengthAwarePaginator對象
$paginate = new LengthAwarePaginator(Array, countOfArray, perPage, currentPage, [
'path' => request()->url(),
'query' => request()->query()
]);
我將currentPage值保留為null。
返回$ paginate查看。
return view('home', ['data' => Array, 'page' => $paginate]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.