Additional Available Settings
Api Auto Pilot provides some additional settings that you can opt in, for some endpoints.
Pagination
In real world applications, there might be thousands or millions of records of a model in the database. Fetching all the records at once would cause server crashes and slow response time requests. That's why you can set up pagination for your desired models.
To set up pagination within Api Auto Pilot, head to the configuration file of the package config/autopilot-api.php
. In the settings key, append the desired model class with a new array, with key pagination
and value the number of records you want to return in each page.
Let's see an example using the Post
model we have used before.
// /config/autopilot-api.php
'settings' => [
Post::class => [
'pagination' => 3
]
]
Now, making a request to get all the posts would result in this response
{
"current_page": 1,
"data": [
{
"id": 4,
"title": "Natus sed asperiores vero distinctio eos sequi voluptatem. Ut odio dicta culpa ut.",
"body": "Eaque quas adipisci ea voluptate assumenda qui exercitationem. Voluptas aut voluptatum iste consequatur doloremque ducimus molestias. Quod fuga quisquam est beatae et. Mollitia deserunt sint qui reprehenderit officia nobis. Aut dolores dolor et perspiciatis. Expedita harum porro quasi excepturi et voluptate. Doloribus minus mollitia aut aut quia aperiam. Exercitationem facilis optio et facere neque. Est et neque et expedita. Dolores aut provident minus dolores aut quam. Rerum ut alias eos ut quibusdam. Vitae adipisci iste repellendus consequatur. Eaque sequi dolorem cumque officiis est. Sunt nulla non provident.",
"created_at": "2022-11-25T04:22:54.000000Z",
"updated_at": "2022-11-25T04:22:54.000000Z"
},
{
"id": 5,
"title": "Fuga architecto et pariatur magni tempore.",
"body": "Voluptatem laborum enim eligendi esse. Excepturi sint voluptatum sunt modi et cum totam. Et dolorem voluptate sapiente. Suscipit praesentium aliquam molestiae sit voluptatibus vero. Et accusantium ut voluptatem alias cupiditate necessitatibus ex. Rerum ea molestiae quo quia error dolores quasi nostrum. Qui vel consequuntur nobis nobis deserunt. Eos qui qui ab voluptatum labore dicta. Cupiditate nesciunt id unde beatae sunt.",
"created_at": "2022-11-25T04:22:54.000000Z",
"updated_at": "2022-11-25T04:22:54.000000Z"
},
{
"id": 6,
"title": "Illum quia quaerat excepturi explicabo consequuntur.",
"body": "Ut est et adipisci accusantium et dignissimos. Dolore rerum adipisci delectus perferendis totam qui magni. Necessitatibus suscipit incidunt et accusantium aliquam distinctio hic et. Corporis et ullam ut quia. Ratione et consequatur esse. In illo vero ipsam possimus temporibus tempore officia. Rerum molestiae velit et ut iure. Dolores temporibus doloribus recusandae sunt sunt repellat in. Dolorem corporis quia aut. Eum nisi consequatur incidunt dolorem doloremque voluptatibus. Quaerat et qui ipsum qui. Eos autem molestias aliquam dolorem aut deleniti. Architecto tenetur iure qui consectetur quam omnis amet.",
"created_at": "2022-11-25T04:22:54.000000Z",
"updated_at": "2022-11-25T04:22:54.000000Z"
}
],
"first_page_url": "http://localhost:8000/api/aap/post?page=1",
"from": 1,
"last_page": 6,
"last_page_url": "http://localhost:8000/api/aap/post?page=6",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://localhost:8000/api/aap/post?page=1",
"label": "1",
"active": true
},
{
"url": "http://localhost:8000/api/aap/post?page=2",
"label": "2",
"active": false
},
{
"url": "http://localhost:8000/api/aap/post?page=3",
"label": "3",
"active": false
},
{
"url": "http://localhost:8000/api/aap/post?page=4",
"label": "4",
"active": false
},
{
"url": "http://localhost:8000/api/aap/post?page=5",
"label": "5",
"active": false
},
{
"url": "http://localhost:8000/api/aap/post?page=6",
"label": "6",
"active": false
},
{
"url": "http://localhost:8000/api/aap/post?page=2",
"label": "Next »",
"active": false
}
],
"next_page_url": "http://localhost:8000/api/aap/post?page=2",
"path": "http://localhost:8000/api/aap/post",
"per_page": 3,
"prev_page_url": null,
"to": 3,
"total": 16
}