This is a tutorial how to use pagination in Laravel. Inside PostsController:
1 2 3 4 5 6 |
public function index() { $posts = Post::paginate(10); return view('welcome', compact('posts')); } |
Inside blade:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
<!doctype html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css"> <!-- Styles --> <style> html, body { background-color: #fff; color: #636b6f; font-family: 'Nunito', sans-serif; font-weight: 200; height: 100vh; margin: 0; } .full-height { height: 100vh; } .flex-center { align-items: center; display: flex; justify-content: center; } .position-ref { position: relative; } .top-right { position: absolute; right: 10px; top: 18px; } .content { text-align: center; } .title { font-size: 50px; } .links > a { color: #636b6f; padding: 0 25px; font-size: 13px; font-weight: 600; letter-spacing: .1rem; text-decoration: none; text-transform: uppercase; } .m-b-md { /*margin-bottom: 0px;*/ } </style> </head> <body> <div class="container position-ref full-height"> <div class="content"> <div class="title m-b-md"> Pagination in Laravel </div> <div class="panel panel-default"> <div class="panel-heading"> <div> <h5>Posts {!! $posts->total() !!}</h5> </div> </div> <div class="panel-body"> @if(count($posts) > 0) <div class="table-responsive"> <table class="table table-bordered table-hover mb30 text-center"> <thead> <tr> <th class="text-center">ID</th> <th class="text-left">Title</th> <th class="text-left">Content</th> <th class="text-center">Image</th> <th class="text-center">Author</th> <th class="text-center">Created at:</th> </tr> </thead> <tbody> @foreach($posts as $post) <tr> <td>{!! $post->id !!} </td> <td class="text-left"> {!! $post->title !!}</td> <td class="text-left"> {!! $post->content !!}</td> <td class="text-left"> <img src="img/{{ $post->image }}" style="width: 30px; height: 30px;" /></td> <td class="text-left"> {!! $post->author_name !!}</td> <td class="text-left"> {!! $post->created_at!!}</td> </tr> @endforeach </tbody> </table> </div> {!! $posts->appends(Request::all())->render()!!} <div class="well"> <ul class="list-unstyled"> <li> <strong>Total posts: {!! $posts->total() !!}</strong> </li> <li> <strong>In this page: {!! $posts->count() !!}</strong> </li> <li> <strong>Total Pages: {!! $posts->lastPage() !!}</strong> </li> </ul> </div> @else <div class="well"> <h4>Posts list is empty</h4> </div> @endif </div> </div> </div> </div> </body> </html> |