kvads's avatar
Level 1

Paginate not working while returning resource collection. What I'm doing wrong?

Hi, I'm trying to return resource collection. Pagination works in this case

$journalQuery = new JournalQuery($validated);
$transactions = $journalQuery->paginate();

but not in this (there is no meta in result):

$journalQuery = new JournalQuery($validated);
$transactions = JournalResource::collection($journalQuery->paginate());

According to docs, that should work. What's wrong?

Resource:

<?php

namespace App\Http\Resources;


use Illuminate\Http\Resources\Json\JsonResource;

class JournalResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     */
    public function toArray($request)
    {

        return [
            'id' => $this->id,
            'amount' => $this->amount,
            'operation_type' => $this->operation_type,
            'bank_account_id' => $this->bank_account_id,
            'accrual_date' => $this->accrual_date,
            'payment_date' => $this->payment_date,
            'bound' => $this->boundMove,
            'accounting_item' => new TransactionAccountingItemResource($this->accountingItem),
            'counterparty' => new CounterpartyResource($this->counterparty),
            'child' => $this->when($this->childItems->count() > 0, self::collection($this->childItems))
        ];
    }
}
0 likes
3 replies

Please or to participate in this conversation.