cskiwi's avatar

adding Pagination messes up the output

hi,

I'm trying to get some chats from my DB. but when adding pagination it messes up the output:

So by using the following code, I get the following output:

code

    echo 'First ouptut';
    $chats = Auth::User()->Chats;
    var_dump(ChatTransformer::transformCollection($chats));

    echo 'Second ouptut';
    $limit = Input::get( 'limit', '5' );
    $chats = Auth::User()->Chats()->paginate( $limit );
    var_dump(ChatTransformer::transformCollection($chats));

output

First ouptut
array (size=2)
  0 => 
    array (size=4)
      'chat_id' => int 9
      'title' => string 'TestChat' (length=8)
      'messages' => 
        array (size=1)
          0 => 
            array (size=6)
              ...
      'users' => 
        array (size=3)
          0 => 
            array (size=3)
              ...
          1 => 
            array (size=3)
              ...
          2 => 
            array (size=3)
              ...
  1 => 
    array (size=4)
      'chat_id' => int 10
      'title' => string 'second Chat' (length=11)
      'messages' => 
        array (size=0)
          empty
      'users' => 
        array (size=3)
          0 => 
            array (size=3)
              ...
          1 => 
            array (size=3)
              ...
          2 => 
            array (size=3)
              ...
Second ouptut
array (size=2)
  0 => 
    array (size=4)
      'chat_id' => int 25
      'title' => string 'TestChat' (length=8)
      'messages' => 
        array (size=0)
          empty
      'users' => 
        array (size=0)
          empty
  1 => 
    array (size=4)
      'chat_id' => int 28
      'title' => string 'second Chat' (length=11)
      'messages' => 
        array (size=0)
          empty
      'users' => 
        array (size=0)
          empty

Notice how the Id's have changed and the output of the users and messages have gone emtpy? any reason why that is happeing?

Note: The id's have change to: chat_users table (that keeps track of what users belongs to what chat group)

0 likes
5 replies
pmall's avatar

A Collection and a Paginator doesn't have the same structure. Not sure if your ChatTransformer can work the same way on both. Can you dump the collection (first case) and the paginator (second case) and the content of your ChatTransformer ?

cskiwi's avatar

Dumps

First ouptut
object(Illuminate\Database\Eloquent\Collection)[287]
  protected 'items' => 
    array (size=1)
      0 => 
        object(App\Chat)[286]
          protected 'fillable' => 
            array (size=1)
              ...
          protected 'connection' => null
          protected 'table' => null
          protected 'primaryKey' => string 'id' (length=2)
          protected 'perPage' => int 15
          public 'incrementing' => boolean true
          public 'timestamps' => boolean true
          protected 'attributes' => 
            array (size=4)
              ...
          protected 'original' => 
            array (size=6)
              ...
          protected 'relations' => 
            array (size=1)
              ...
          protected 'hidden' => 
            array (size=0)
              ...
          protected 'visible' => 
            array (size=0)
              ...
          protected 'appends' => 
            array (size=0)
              ...
          protected 'guarded' => 
            array (size=1)
              ...
          protected 'dates' => 
            array (size=0)
              ...
          protected 'touches' => 
            array (size=0)
              ...
          protected 'observables' => 
            array (size=0)
              ...
          protected 'with' => 
            array (size=0)
              ...
          protected 'morphClass' => null
          public 'exists' => boolean true
Second ouptut
object(Illuminate\Pagination\Paginator)[289]
  protected 'hasMore' => boolean false
  protected 'items' => 
    object(Illuminate\Support\Collection)[291]
      protected 'items' => 
        array (size=1)
          0 => 
            object(App\Chat)[290]
              ...
  protected 'perPage' => string '5' (length=1)
  protected 'currentPage' => int 1
  protected 'path' => string 'http://unicorn.app:8000/api/chats/' (length=34)
  protected 'query' => 
    array (size=0)
      empty
  protected 'fragment' => null
  protected 'pageName' => string 'page' (length=4)

my ChatTransformer

<?php
namespace App\Transformers;

use App\Chat;
use Doctrine\Instantiator\Exception\InvalidArgumentException;
use Illuminate\Database\Eloquent\Model;

class ChatTransformer extends Transformer {

  public static function transform(Model $item, $args = [ ]) {
    if (!( $item instanceof Chat )) throw new InvalidArgumentException;

    $return = array();

    $return['chat_id'] = $item->id;
    $return['title'] = $item->title;

    // $firstMessage = Auth::User()->Chats()->first()->Messages()->first();

    $return['messages'] = ( new MessageTransformer() )->transformCollection( $item->messages()->take( 20 )->get() );
    $return['users'] = ( new UserTransformer() )->transformCollection( $item->users );

    return $return;
  }
}

That is extending the Trasnformer:

<?php
namespace App\Transformers;

use Illuminate\Database\Eloquent\Model;

abstract class Transformer {
  public static function transformCollection($items, $args = [ ]) {
    $var = array();
    foreach ($items as $item) {
      array_push( $var, static::transform( $item, $args ) );
    }
    return $var;
  }

  public static function transform(Model $item, $args = [ ]) { }
}

The transformer is being used for all the output everywhere, and each model has it's own Transformer, it's the first time it's having issues

And the transformer works if I don't paginate it (what you can see in first post)

nolros's avatar

I was messing around with this yesterday. I don't think you can have limit and paginator on the same query. I think it is either one. When I added limit it started loading the collection class.

1 like
foxted's avatar

For your paginated output, you are passing the Paginator object instead of the collection:

echo 'Second ouptut';
$limit = Input::get( 'limit', '5' );
$chats = Auth::User()->Chats()->paginate( $limit );
var_dump(ChatTransformer::transformCollection($chats));

Maybe try it this way:

echo 'Second ouptut';
$limit = Input::get( 'limit', '5' );
$chats = Auth::User()->Chats()->paginate( $limit );
var_dump(ChatTransformer::transformCollection($chats->getCollection()));

Source: http://laravel.com/api/4.1/Illuminate/Pagination/Paginator.html#method_getCollection

cskiwi's avatar
cskiwi
OP
Best Answer
Level 2

Believe it or not it just started working today, didn't change anything. But now it's showing as it should.

Thanks for the help anyway!

1 like

Please or to participate in this conversation.