jeudi 31 janvier 2019

Missing pagination with Resource in CollectionResource

I'm trying to get a paginated CollectionResource after to use a Resource in collection, but the paginate data is always missing.

BillController :

$bill = Bill::with('category');    
return new BillCollectionResource( BillResource::collection($bill->paginate(10)) );

BillResource

public function toArray($request)
{   
    $data = parent::toArray($request);
    $data['lorem'] = 'ipsum';
    $data['foo'] = 'bar';
    return $data;
}

BillCollectionResource

public function toArray($request)
{
return [
    'data'                  => $this->collection,
    'rows_count'            => $rows_count,
    'total_balance'         => $total_balance,
    'total_num_debits'      => $total_num_debits,
    'total_num_credits'     => $total_num_credits,
    'total_balance_debits'  => $total_balance_debits,
    'total_balance_credits' => $total_balance_credits,

    ];
}

Expected results:

{
  data:                 [...],
  rows_count            : 16,
  total_balance         : 200.00,
  total_num_debits      : 8,
  total_num_credits     : 4,
  total_balance_debits  : -100.00,
  total_balance_credits : 100.00,

   links: 
    {
        first: 'http://localhost:8001/api/...',
        last:  'http://localhost:8001/api/...',
        next:  'http://localhost:8001/api/..',
        prev:  null,
    },
    meta: 
    {
        to:             5,
        from:           1,
        path:           'http://localhost:8001/api/...',
        total:          75
        per_page:       5,
        last_page:      15,
        current_page:   1,
    }

}

Current results:

{
  data:                 [...],
  rows_count            : 16,
  total_balance         : 200.00,
  total_num_debits      : 8,
  total_num_credits     : 4,
  total_balance_debits  : -100.00,
  total_balance_credits : 100.00,
}

I tried to add the pagination manually too, but all $request methods below are undefined.

BillCollectionResource

        'pagination' => 
         [
            'total'         => $request->total(),
            'count'         => $request->count(),
            'per_page'      => $request->perPage(),
            'current_page'  => $request->currentPage(),
            'total_pages'   => $request->lastPage()
         ]

What can be the reason behind this behaviour? Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire