Developer Guideline
  • Home
  • 🧠Must you know
    • Algorithm
    • Architecture
      • API
    • Comparison
      • ID Token vs Access Token
      • Lazy Loading vs Eager Loading
      • Morphs vs Foreign Key
      • UUID vs ULID
      • GraphQL vs REST
      • Cache vs CDN
      • Concurrency vs Parallelism
      • Null vs Not Null
    • Diagram
      • CI/CD Pipeline
      • High Performance Culture
  • ☂️Laravel
    • Template
      • Template System Check in Route on Laravel
      • Template Function in FormController on Laravel
      • Template Route call FormController on Laravel
      • Template Route Prefix Name on Laravel
      • Template Basic and Custom Pagination on Laravel
      • Template PHP Artisan Command
      • Template Route for App Environment
    • Feature
      • Data Type
      • Mailables
      • Rules
    • Package
    • Document
  • 🫖Collaboration Agreement
    • Naming Convention
      • Naming Convention for Git Branch
      • Naming Convention for Environment Variable
    • Rule
      • Rule of Commit Message
      • Semantic Versioning
  • 🦣Project Manager
    • Requirements
      • System Requirements
      • Technical Requirements
      • Functional Requirements
Powered by GitBook
On this page
  • 🐤 Basic
  • 🦁 Custom
  1. Laravel
  2. Template

Template Basic and Custom Pagination on Laravel

ตัวอย่าง Pagination แบบ Basic ที่ใช้กันทั่วไป ร่วมกับ CSS Framework อย่าง Bootstrap, Tailwind กับแบบที่ต้อง Custom เอง

🐤 Basic

PaginationController.php
public function index()
{
   $posts = Post::paginate(10);
   return view('posts.index', ['posts' => $posts]);
}
pagination.blade.php
@foreach ($posts as $post)
    <div class="post">
        <h2>{{ $post->title }}</h2>
        <p>{{ $post->content }}</p>
    </div>
@endforeach

{{ $posts->links() }}

🦁 Custom

PaginationController.php
public function index()
{
    $perPage = 3;
    $obj = Project::where('status', '!=', null)->paginate($perPage);
    $totalPages = ceil($obj->total() / $perPage);
    return view('admin.project.index', compact('obj', 'totalPages'));
}
pagination.blade.php
@if ($obj->hasPages())
<nav>
    <ul class="pagination justify-content-center">
        {{-- Previous Page Link --}}
        @if ($obj->onFirstPage())
            <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
                <span class="page-link" aria-hidden="true">&lsaquo;</span>
            </li>
        @else
            <li class="page-item">
                <a class="page-link" href="{{ $obj->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">&lsaquo;</a>
            </li>
        @endif

        {{-- Pagination Elements --}}
        @for ($i = 1; $i <= $totalPages; $i++)
            <li class="page-item"><a class="page-link" href="{{ url('admin/project?page=' . $i) }}">{{ $i }}</a></li>
        @endfor

        {{-- Next Page Link --}}
        @if ($obj->hasMorePages())
            <li class="page-item">
                <a class="page-link" href="{{ $obj->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">&rsaquo;</a>
            </li>
        @else
            <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
                <span class="page-link" aria-hidden="true">&rsaquo;</span>
            </li>
        @endif
    </ul>
</nav>
@endif
PreviousTemplate Route Prefix Name on LaravelNextTemplate PHP Artisan Command

Last updated 10 months ago

☂️