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'));
}

Last updated