Laravel 5 API Pagination - How to Paginate Laravel 5 Api



Download Brave Browser


How to Paginate Laravel 5 Api


I needed to pull some info from an API to an Android mobile phone and because I was using a framework that is a bit tricky to use(called Framework 7), I realised that paginating my results from the mobile rather than the API itself was going to be quiet hard.


Here is how you will manage to have this in your API:
    "links": {
        "first": "https://yourwebsite.com/api/gettextimages?page=1",
        "last": "https://yourwebsite.com/api/gettextimages?page=3",
        "prev": null,
        "next": "https://yourwebsite.com/api/gettextimages?page=2"
By having this extra metadata you will be able to browse all the records in the API page by page, Neat!

Let's start!

What will we create with Laravel 5.5 in this tutorial?

a)We will create a resource template which will be used to model nicely our API
b)We will write some code in our controller to pull the data into our new resource template
c)First we will pull only single details
d)We will then create the logic to query a collection of items rather than single items
e)We wil paginate the results
c)We will hide certain details we do not want to show in our API



1)Start your Node.js command prompt and type this command:
php artisan make:resource User

The file will be generated at this location:
App/Http/Resources/User.php


2)Go to your user controller and add the below code:

Top of the file:
use App\Http\Resources\User as UserResource
The reason we have renamed User to UseResource is because the User Model already exists and this will conflict with the internal of Laravel's own logic

Add this to the function you will use to pull the User records:

$user = User::find(1);
return new UserResource($user);

3)Go to your User resource template file and change the code as required to great a nice API metadata organization:


    public function toArray($request)
    {
         return [
             'id' => $this->id,
             'name' => $this->name,
        ];
    }

Remember that these names should match the field names you have in your user controller.

4)Try to access your API and see if the results show up.



How to Query a Collection of Items and Paginate the API?


1)First, let us create a resource collection by typing this command in our node.js prompt:


php artisan make:resource UserCollection


The file will be generated at this location:
App/Http/Resources/UserCollection.php

2)Go to your user controller and add the below code:

Top of the file:
use App\Http\Resources\UserCollection

Add this to the function you will use to pull the User records:

$user = User::paginate(10)
return new UserCollection($user);


3)In your UserCollection.php resource file leave the code as it is:

    public function toArray($request)
    {
        return parent::toArray($request);
    }

4)Finally to hide certain details in your API from your users add this in your User Model:

 protected $hidden = ['password', 'remember_token',];


This is, of course, a demo using the User model but you can use this example for any of your Laravel Models. Simply use the same steps as above and all your APIs will look gorgeous!


Have fun and please leave us a message if this tutorial helped you.



";