![](/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.