Pagination is used to split large amounts of data into smaller parts (pages). This helps make the website faster and easier to use.
In the controller, you can use Laravel’s built-in paginate()
method to paginate your products. Example:
useApp\Models\Product;
public function index()
{
$products = Product::paginate(10); // 10 products per page
view('product.index', compact('products'));
}
In the Blade view fileproduct/index.blade.php
, you can show the products like this:
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach( $products as $product )
<tr>
<td>{{ $product->id }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->price }}</td>
</tr>
@endforeach
</tbody>
</table>
<!-- Pagination Links -->
{{ $products->links() }}