Don't know this package at all, but it seems like it is using Fractal to transform the paginator, there must be a way for you to override the transformer.
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?
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
Please or to participate in this conversation.