laravel-woocommerce icon indicating copy to clipboard operation
laravel-woocommerce copied to clipboard

Pagination

Open andreasemprofile opened this issue 4 years ago • 7 comments

Hello everybody, can someone with a kind soul help me understand how to make a pagination of orders with laravel 8? I've followed the documentation but can't. More precisely I can only set how many orders I want per page but I cannot figure out how to use $ orders ['meta] with the links () method of blade.

This is my controller:

image

this is my blade view

image

this is the error

Call to a member function links() on array (View: C:\Users\andrea\Desktop\developer\semprofile\ipanel\epanel\resources\views\Orders\index.blade.php)

andreasemprofile avatar Feb 17 '21 11:02 andreasemprofile

Hello @andreasemprofile, Thanks for creating the issue. Actually, you can't use the links() method because it doesn't exist in this package. You can get paginate data in the meta section. I'll try to implement the link method like Laravel paginate.

Thanks to the catching this. We will include it in the next version.

maab16 avatar Feb 17 '21 13:02 maab16

Hi maab16 , thank you so much for the super fast support. Can you please provide a sample code to use for pagination?

andreasemprofile avatar Feb 17 '21 13:02 andreasemprofile

If you check to paginate method then you can get an idea of how it works. When you call paginate() then it returns an object with two property meta and data. Meta contains all information related to the p[agination and data return your model (Order, Product, etc) data.

/**
     * Paginate results.
     *
     * @param int   $per_page
     * @param int   $current_page
     * @param array $options
     *
     * @return array
     */
    protected function paginate($per_page = 10, $current_page = 1, $options = [])
    {
        try {
            $this->options['per_page'] = (int) $per_page;

            if ($current_page > 0) {
                $this->options['page'] = (int) $current_page;
            }

            foreach ($options as $option => $value) {
                $this->options[$option] = $value;
            }

            $data = $this->get();
            $totalResults = WooCommerce::countResults();
            $totalPages = WooCommerce::countPages();
            $currentPage = WooCommerce::current();
            $previousPage = WooCommerce::previous();
            $nextPage = WooCommerce::next();

            $pagination = [
                'total_results' => $totalResults,
                'total_pages'   => $totalPages,
                'current_page'  => $currentPage,
                'previous_page' => $previousPage,
                'next_page'     => $nextPage,
                'first_page'    => 1,
                'last_page'     => $totalResults,
            ];

            $results = [
                'meta'       => $pagination,
                'data'       => $data,
            ];

            if ($this->isLazyCollection) {
                return LazyCollection::make($results);
            }

            if ($this->isCollection) {
                return collect($results);
            }

            return $results;
        } catch (\Exception $ex) {
            throw new \Exception($ex->getMessage(), 1);
        }
    }

maab16 avatar Feb 17 '21 13:02 maab16

Thanks so much for your support and I hope the links () method will be integrated soon. Good job

andreasemprofile avatar Feb 17 '21 13:02 andreasemprofile