1$category = Category::first();
2$apps = $category->apps()->paginate(10);
3return view('example', compact('category', 'apps'));
1# use default 'page' for this
2$collection1 = Model::paginate(20);
3
4# use custom 'other_page' for this
5$collection2 = Model2::paginate(20);
6$collection2->setPageName('other_page');
1<?php
2
3namespace App\Http\Controllers;
4
5use Illuminate\Http\Request;
6use Illuminate\Pagination\Paginator;
7use Illuminate\Support\Collection;
8use Illuminate\Pagination\LengthAwarePaginator;
9
10class PaginationController extends Controller
11{
12 /**
13 * The attributes that are mass assignable.
14 *
15 * @var array
16 */
17 public function index()
18 {
19 $myArray = [
20 ['id'=>1, 'title'=>'Laravel 6 CRUD'],
21 ['id'=>2, 'title'=>'Laravel 6 Ajax CRUD'],
22 ['id'=>3, 'title'=>'Laravel 6 CORS Middleware'],
23 ['id'=>4, 'title'=>'Laravel 6 Autocomplete'],
24 ['id'=>5, 'title'=>'Laravel 6 Image Upload'],
25 ['id'=>6, 'title'=>'Laravel 6 Ajax Request'],
26 ['id'=>7, 'title'=>'Laravel 6 Multiple Image Upload'],
27 ['id'=>8, 'title'=>'Laravel 6 Ckeditor'],
28 ['id'=>9, 'title'=>'Laravel 6 Rest API'],
29 ['id'=>10, 'title'=>'Laravel 6 Pagination'],
30 ];
31
32 $myCollectionObj = collect($myArray);
33
34 $data = $this->paginate($myCollectionObj);
35
36 return view('paginate', compact('data'));
37 }
38
39 /**
40 * The attributes that are mass assignable.
41 *
42 * @var array
43 */
44 public function paginate($items, $perPage = 5, $page = null, $options = [])
45 {
46 $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
47 $items = $items instanceof Collection ? $items : Collection::make($items);
48 return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
49 }
50}
51
52# Blade file
53<div class="container">
54 <table class="table table-bordered">
55 <tr>
56 <th>Id</th>
57 <th>Title</th>
58 </tr>
59 @foreach($data as $post)
60 <tr>
61 <td>{{ $post->id }}</td>
62 <td>{{ $post->title }}</td>
63 </tr>
64 @endforeach
65 </table>
66</div>
67
68{{ $data->links() }}
1
2@if ($paginator->hasPages())
3 <ul class="pager">
4
5 @if ($paginator->onFirstPage())
6 <li class="disabled"><span>← Previous</span></li>
7 @else
8 <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">← Previous</a></li>
9 @endif
10
11
12
13 @foreach ($elements as $element)
14
15 @if (is_string($element))
16 <li class="disabled"><span>{{ $element }}</span></li>
17 @endif
18
19
20
21 @if (is_array($element))
22 @foreach ($element as $page => $url)
23 @if ($page == $paginator->currentPage())
24 <li class="active my-active"><span>{{ $page }}</span></li>
25 @else
26 <li><a href="{{ $url }}">{{ $page }}</a></li>
27 @endif
28 @endforeach
29 @endif
30 @endforeach
31
32
33
34 @if ($paginator->hasMorePages())
35 <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">Next →</a></li>
36 @else
37 <li class="disabled"><span>Next →</span></li>
38 @endif
39 </ul>
40@endif
1<?php
2// config
3$link_limit = 7; // maximum number of links (a little bit inaccurate, but will be ok for now)
4?>
5
6@if ($paginator->lastPage() > 1)
7 <ul class="pagination">
8 <li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}">
9 <a href="{{ $paginator->url(1) }}">First</a>
10 </li>
11 @for ($i = 1; $i <= $paginator->lastPage(); $i++)
12 <?php
13 $half_total_links = floor($link_limit / 2);
14 $from = $paginator->currentPage() - $half_total_links;
15 $to = $paginator->currentPage() + $half_total_links;
16 if ($paginator->currentPage() < $half_total_links) {
17 $to += $half_total_links - $paginator->currentPage();
18 }
19 if ($paginator->lastPage() - $paginator->currentPage() < $half_total_links) {
20 $from -= $half_total_links - ($paginator->lastPage() - $paginator->currentPage()) - 1;
21 }
22 ?>
23 @if ($from < $i && $i < $to)
24 <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
25 <a href="{{ $paginator->url($i) }}">{{ $i }}</a>
26 </li>
27 @endif
28 @endfor
29 <li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}">
30 <a href="{{ $paginator->url($paginator->lastPage()) }}">Last</a>
31 </li>
32 </ul>
33@endif
34