PetroGromovo's avatar

Error Collection::appends does not exist In pagination with mapping

Hello!

In my Laravel 5.8 app I have database request with mapping, like:

        $paymentItems        = PaymentItem
            ::getByStatus('C', 'payments')
            ->select(
                $this->payment_items_tb . '.*',
                $this->downloads_tb . '.title as payed_item_title',
                $this->users_tb     . '.username as payment_username',
                $this->payments_tb . '.payment_type',
                ...
                $this->payments_tb . '.payer_shipping_address'
            )
            ->orderBy($this->payment_items_tb . '.created_at', 'desc')
            ->join($this->payments_tb, $this->payments_tb . '.id', '=', $this->payment_items_tb . '.payment_id')
            ->join($this->users_tb, $this->users_tb . '.id', '=', $this->payments_tb . '.user_id')
            ->join($this->downloads_tb, $this->downloads_tb . '.id', '=', $this->payment_items_tb . '.item_id')
            ->get()
            ->map(function ($item) {
                return [
                    'id'               => $item->id,
                    'total'            => $item->price * $item->quantity,
                        ....
                    'payment_type_label'     => Payment::getPaymentTypeLabel($item->payment_type),
                ];
            });

It worked ok for me untill I decided to remake this listing as pagination, like:

        $paymentItems        = PaymentItem
            ::getByStatus('C', 'payments')
            ->select(
                $this->payment_items_tb . '.*',
                $this->downloads_tb . '.title as payed_item_title',
                $this->users_tb     . '.username as payment_username',
                $this->payments_tb . '.payment_type',
                ...
                $this->payments_tb . '.payer_shipping_address'
            )
            ->orderBy($this->payment_items_tb . '.created_at', 'desc')
            ->join($this->payments_tb, $this->payments_tb . '.id', '=', $this->payment_items_tb . '.payment_id')
            ->join($this->users_tb, $this->users_tb . '.id', '=', $this->payments_tb . '.user_id')
            ->join($this->downloads_tb, $this->downloads_tb . '.id', '=', $this->payment_items_tb . '.item_id')
            ->paginate($ref_items_per_pagination, null, null, $page)
            ->onEachSide((int)($ref_items_per_pagination / 2))

            ->map(function ($item) {
                return [
                    'id'               => $item->id,
                    'total'            => $item->price * $item->quantity,
                    ...
                    'payment_type_label'     => Payment::getPaymentTypeLabel($item->payment_type),
                ];
            });

But In my blade template I got error :

Method Illuminate\Support\Collection::appends does not exist. (View: /resources/views/defaultBS41Backend/admin/dashboard/payments_rows.blade.php) {"userId":5,"exception":"[object] (ErrorException(code: 0): Method Illuminate\Support\Collection::appends does not exist. (View: /resources/views/defaultBS41Backend/admin/dashboard/payments_rows.blade.php) at /vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php:102, BadMethodCallException(code: 0): Method Illuminate\Support\Collection::appends does not exist.
trying to show pagination links block:
            <div class="row">
                {{ $paymentItems->appends([])->links() }}
                <input type="button" class="btn btn-primary" value="Refresh" onclick="javascript:backendDashboard.showPaymentsRows(1); return false;">
        </div>

Is there is a way to fix this error and to show paginations links ?

Thanks!

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Its because your map statement removes the paginator. Do you really need to do that or can you calculate the total in the view?

1 like

Please or to participate in this conversation.