henninghorn's avatar

Best way to "rewrite" paginate response?

I've been trying to implement the "Laravel 5 Repositories" package: https://github.com/andersao/l5-repository

So far, so good.

If I just return $this->repository->paginate() I get the data in an array named "data" and the pagination stuff in some other fields. I don't like the way it's formatted and I don't need the fields "next_page_url" and "prev_page_url".

What's the best way to filter the standard response?

0 likes
6 replies
andersonandrade's avatar

Hi, @henninghorn !

Paging data is coming this way because of the Fractal use (https://github.com/andersao/l5-repository#presenters) , but the use of it is optional.

Just in your Repository class, remove the method presenter, and presto, you have the raw data.

Another option is skip the presenter with skipPresenter () method ( https://github.com/andersao/l5-repository#skip-presenter-defined-in-the-repository )

use Prettus\Repository\Eloquent\BaseRepository;

class PostRepository extends BaseRepository {

    ...
   // Remove this method
    public function presenter()
    {
        return "Prettus\\Repository\\Presenter\\ModelFractalPresenter";
    }
}

Or in your controller use this:

$this->repository->skipPresenter()->paginate() 
1 like
henninghorn's avatar

Hi @andersonandrade

Thank you for your reply, and sorry about this late answer.

I'm not sure how the presenters could fix my problem. My problem is that, when you paginate data, the data is grouped in a certain way, I want to change that default behaviour.

As I understand the presenters, they only deal with the models data, not the extra fields getting added by the pagination. Or have I misunderstood something here?

andersonandrade's avatar
Level 1

Hello @henninghorn , sorry for the delay in responding. Well, I do not know if I understand what you want, but follows some ways of how you can use the presenters to display your data.

By default, the package exists a presenter using the fractal transform of the package model data and display more friendly form, which is widely used for the development of APIs.

But you can create your own host or use other libraries, you only need implemtentar an interface. Prettus \ Repository \ Contracts \ PresenterInterface

Here some examples of how you could implement.

No presenters

You can not use presenters and return the standard Laravel data.

Example Response:

{
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1,
    next_page_url: null,
    prev_page_url: null,
    from: 1,
    to: 1,
    data: [
        {
            id: 1,
            login: "andersonandrade",
            email: "contato@andersonandra.de",
            created_at: "2015-04-15 17:13:37",
            updated_at: "2015-04-15 17:13:37",
            deleted_at: null
        }
    ]
}

Fractal presenter

You can use the use the Fractal presenter

https://github.com/andersao/l5-repository#fractal-presenter

Example Response:

{
    data: [
        {
            id: 1,
            login: "andersonandrade",
            email: "contato@andersonandra.de",
            created_at: "2015-04-15 17:13:37",
            updated_at: "2015-04-15 17:13:37",
            deleted_at: null
        }
    ],
    meta: {
        pagination: {
            total: 1,
            count: 1,
            per_page: 15,
            current_page: 1,
            total_pages: 1,
            links: [ ]
        }
    }
}

Custom presenter

Or you can create your custom presenter

<?php namespace App\Presenters;

use Illuminate\Contracts\Support\Arrayable;
use Prettus\Repository\Contracts\PresenterInterface;

class MyPresenter implements PresenterInterface {

    /**
     * Prepare data to present
     *
     * @param $data
     * @return mixed
     */
    public function present($data)
    {
        if( $data instanceof Arrayable )
        {
            $array = $data->toArray();
            return $array['data'];
        }

        return $data;
    }
}

Example Response:

[
    {
        id: 1,
            login: "andersonandrade",
            email: "contato@andersonandra.de",
            created_at: "2015-04-15 17:13:37",
            updated_at: "2015-04-15 17:13:37",
            deleted_at: null
    }
]

Hope this helps

1 like
henninghorn's avatar

@andersonandrade

Thank you for your reply, that definitely gives something to work with. I had some trouble getting my head wrapped around all of it.

Once more, thank you!

Please or to participate in this conversation.