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