-
-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API resources don't include pagination data when passed to Inertia render #28
Comments
Here's both Resource classes, @adriandmitroca.
Make sure you're extending |
Yeah, that's basically exactly what I have. Is it working for you? |
I see the issue - Laravel returns huge object of :/ |
Folks, I'm not sure if this is helpful because I haven't read through the whole discussion, but I also ran into some issues with Laravel's pagination...with is very much designed for Blade/servers-side rendering. I ended up creating my own (somewhat complex) pagination class. See here: You might want to try that to see if it helps in your situation. |
@reinink Ah shoot, that's probably what @adriandmitroca is missing. I forgot I borrowed that from PingCRM a while back. I still wind up having to do the |
This is definitely not my day - I've already tried to borrow Jonathan's workaround and I'm triggering it within register() method in AppServiceProvider but nothing changes - still HTML. Losing my mind :( |
@reinink So, I've done a bit of digging, and I'm able to get the
It's not super pretty, but it does get us all the data the The results: Controller call:
Response:
(This leaves generating the list of page links to the JS side. Something like this component would probably do the trick.) |
I've gave it further thought and unfortunately it is impossible to pass current query parameters to a pagination created on the fly by API Resources. I guess the best way way to do it is basically what Jonathan did in PingCRM app and overwriting pagination class to own needs. |
We are using a helper function for it: /**
* Gather the meta data for the response.
*
* @param LengthAwarePaginator $paginated
* @return array
*/
function pagination($paginated)
{
return [
'current' => $paginated->currentPage(),
'last' => $paginated->lastPage(),
'base' => $paginated->url(1),
'next' => $paginated->nextPageUrl(),
'prev' => $paginated->previousPageUrl()
];
} example of usage: return Inertia::render('Foo', [
'foo' => FooResource::collection(Foo::paginate()),
'pagination' => pagination($foos),
]); |
We are using a custom pagination vue components and this is how we have implemented public function index()
{
return Inertia::render('Users/Index', [
'users' => new UserCollection(User::paginate(10))
]);
} class UserCollection extends ResourceCollection
{
/**
* The resource that this resource collects.
*
* @var string
*/
public $collects = UserResource::class;
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function toArray($request) {
return [
'data' => $this->collection,
'pagination' => [
'size' => $this->perPage(),
'total' => $this->total(),
'current' => $this->currentPage(),
// customise your pagination here
// https://laravel.com/docs/5.8/pagination#paginator-instance-methods
],
];
}
} class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function toArray($request) {
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'profile' => new ProfileResource($this->whenLoaded('profile'))
];
}
} |
Hey folks! I think this is probably best left out of this adapter. There seems to be different ways to solve this (admittedly) annoying problem. The whole issue is that the current I hope to eventually make a PR to Laravel that adds And, I think either way you're going to need to a pagination component on the front-end. However, that's pretty easy to put together. You can see the Ping CRM one here. |
I've added support for "Responsable" props: 2b966ac This will cause the additional resource information (ie. pagination data) to be included with your responses. I've tested this with both single resource responses and collection resource responses, and it works great. 👍 |
Pagination data in API resources appears to be handled by the
toResponse
function ofIlluminate\Http\Resources\Json\PaginatedResourceResponse
.That means this:
gets the correctly transformed collection, but it's missing the
links
(and other) metadata.I'm able to work around it by doing this:
and having FooCollection do this:
but it's not as elegant as the default behavior in Laravel.
The text was updated successfully, but these errors were encountered: