简体   繁体   English

在Laravel 5.6中的数组上调用成员函数links()

[英]Call to a member function links() on array in Laravel 5.6

I am using pagination in Laravel 5.6 to build an application. 我在Laravel 5.6中使用分页来构建应用程序。 I am using Laravel DB instead of Eloquent. 我正在使用Laravel DB而不是Eloquent。 However, when I add a pagination link on view, it shows an error Call to a member function links() on array . 但是,当我在视图上添加分页链接时,它在array上显示对成员函数links()的调用错误。 This is because I am using an array, the default is an object with a collection. 这是因为我使用的是数组,默认值为带有集合的对象。 The output I am getting looks like 我得到的输出看起来像

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
]

When I use normal pagination ie without converting to an array, the output is 当我使用常规分页,即不转换为数组时,输出为

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"
}

The problem I face here is adding other data to collection 我在这里面临的问题是将其他数据添加到集合中

I can't use the default output return by pagination() method. 我不能使用pagination()方法的默认输出返回值。 Can someone please suggest how to solve this issue? 有人可以建议如何解决这个问题吗?

Using the below, I am getting the required output. 使用下面的代码,我得到所需的输出。

Step 1: Include LengthAwarePaginator in the file 步骤1:在文件中包含LengthAwarePaginator

use Illuminate\Pagination\LengthAwarePaginator;

Step 2: Create an object of LengthAwarePaginator 步骤2:创建一个LengthAwarePaginator对象

$paginate = new LengthAwarePaginator(Array, countOfArray, perPage, currentPage, [
        'path' =>  request()->url(),
        'query' => request()->query()
]);

I kept currentPage value as null. 我将currentPage值保留为null。
Return $paginate to view. 返回$ paginate查看。

return view('home', ['data' => Array, 'page' => $paginate]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM