Reputation: 2401
I would like to create a pagination in Rest API for Laravel. I want pagination like, Suppose I have total 30 categories in Database & I want to display 10 items in each page So total page will be 3 and 10 items will display in each page.
I have used paginate()
method but it's only showing items by given number , I used below the script but it's not provided output that I mentioned.
$getCategories = Category::paginate();
$listCat = array();
foreach($getCategories as $val){
$cateName['id'] = $val->id;
$cateName['name'] = $val->name;
$cateName['description'] = $val->description;
$cateName['image'] = url('/').'/public/assets/images/category/'.$val->image;
$listCat[] = $cateName;
}
if(!empty($listCat)){
$respons['status'] = 'true';
$respons['message'] = 'Categories list';
$respons['data list']=$listCat;
}else{
$respons['status'] = 'false';
$respons['message'] = 'Category is not found.';
}
return Response::json($respons,200);
Does anybody tell me how could i use pagination in Rest Api?
I got output like..
Upvotes: 7
Views: 47565
Reputation: 359
you need to turn your pagination or pagination instance to json to get the data of you pagination
just as @Kotzilla answer it for you but i'm providing the answer that worked for me
this is done from your controller or from the place you call your query from . and this is what you did in your example but ....
$getCategories = Category::paginate(10)->response()->getData(true);
this will return your pagination data in your response
Upvotes: 0
Reputation: 378
use LengthAwarePaginator to generate the laravel pagination links here is an example:
use Illuminate\Pagination\LengthAwarePaginator;
then:
public function getPosts(Request $request){
//First resolve the current page
$page = LengthAwarePaginator::resolveCurrentPage();
//Define how many items you want to display per page
$perPage = 15;
//Call the external API and retrieve the data passing the $page and the $perPage
$response = Http::get('https://api.example.com/posts', [
'page' => $page,
'per_page' => $perPage,
]);
$data = $response->json();
//Create a new LengthAwarePaginator instance
$paginator = new LengthAwarePaginator($data['items'], $data['total'], $perPage, $page, [
'path' => LengthAwarePaginator::resolveCurrentPath(),
]);
//Pass the paginator instance to your Blade view
return view('items', ['items' => $paginator]);
});
In your Blade view, you can use the $posts variable like a regular paginator instance and use methods like links() to generate pagination links:
<ul>
@foreach ($posts as $post)
<li>{{ $post['name'] }}</li>
@endforeach
</ul>
{{ $posts->links() }}
Upvotes: 2
Reputation: 1413
for REST pagination you can use code like this
$data = Category::paginate(request()->all());
return Response::json($data, 200);
then you will see at the bottom of data, they will show the pagination info for you
Upvotes: 23
Reputation: 3
it's simple
---------use this in your controller---------
$categories = DB::table('category')
->paginate(3);
return view('your_view_here',[
'categories' => $categories,
]);
-----------in your views file----------
@foreach($categories as $category)
{{$category-id}}
@endforeach
<span>{{ $categories->appends(request()->input())->links() }}</span>
the span code is the paginate buttons
hope it will help u
Upvotes: -5
Reputation: 621
After this:
$getCategories = Category::All();
$categories = DB::table('category')->paginate($pageNumber);
Return it as:
return response()->json($categories, 200);
In your json response you will see "next_page_url"
to go second page or vice versa "prev_page_url"
.
Upvotes: -2