简体   繁体   中英

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

I am using pagination in Laravel 5.6 to build an application. I am using Laravel DB instead of Eloquent. However, when I add a pagination link on view, it shows an error Call to a member function links() on array . 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. 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

use Illuminate\Pagination\LengthAwarePaginator;

Step 2: Create an object of LengthAwarePaginator

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

I kept currentPage value as null.
Return $paginate to view.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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