berikut adalah langkah-langkah lebih detail untuk menambahkan pagination ke daftar produk Anda di Laravel:
Langkah 1: Install Paginator
buka file app/Providers/AppServiceProvider.php
dan impor Paginator di dalamnya:
Copy use Illuminate \ Pagination \ Paginator ;
Kemudian, dalam metode boot()
, tambahkan baris berikut untuk menggunakan Bootstrap sebagai tema pagination:
Copy <? php
namespace App \ Providers ;
use Illuminate \ Support \ ServiceProvider ;
use Illuminate \ Pagination \ Paginator ;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register () : void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot () : void
{
Paginator :: useBootstrap () ;
}
}
Di dalam ProductController
, ubah metode index()
untuk menggunakan metode paginate()
:
Copy <? php
namespace App \ Http \ Controllers ;
use App \ Models \ Product ;
use Illuminate \ Http \ Request ;
class ProductController extends Controller
{
public function index ()
{
$products = Product :: paginate ( 5 ) ; // Menampilkan 10 produk per halaman
return view ( 'products.index' , compact ( 'products' )) ;
}
public function create ()
{
return view ( 'products.create' ) ;
}
public function store ( Request $request)
{
$request -> validate ( [
'name' => 'required|string|max:255' ,
'price' => 'required|numeric' ,
'stock' => 'required|integer' ,
] ) ;
Product :: create ( $request -> all ()) ;
return redirect () -> route ( 'products.index' ) -> with ( 'success' , 'Product created successfully' ) ;
}
public function show ($id)
{
$product = Product :: find ( $id ) ; // Mengambil produk berdasarkan ID
return view ( 'products.show' , compact ( 'product' )) ;
}
public function edit ($id)
{
$product = Product :: find ( $id ) ; // Mengambil produk berdasarkan ID
return view ( 'products.edit' , compact ( 'product' )) ;
}
public function update ( Request $request , $id)
{
$request -> validate ( [
'name' => 'required|string|max:255' ,
'price' => 'required|numeric' ,
'stock' => 'required|integer' ,
] ) ;
$product = Product :: find ( $id ) ;
$product -> update ( $request -> all ()) ;
return redirect () -> route ( 'products.index' ) -> with ( 'success' , 'Product updated successfully' ) ;
}
public function destroy ($id)
{
$product = Product :: find ( $id ) ;
$product -> delete () ;
return redirect () -> route ( 'products.index' ) -> with ( 'success' , 'Product deleted successfully' ) ;
}
// Menampilkan daftar produk
public function list_product ()
{
$products = Product :: all () ;
return response () -> json ( $products ) ;
}
// Menampilkan detail produk berdasarkan ID
public function detail_product ($id)
{
$product = Product :: find ( $id ) ;
if ( ! $product) {
return response () -> json ( [ 'message' => 'Produk tidak ditemukan' ] , 404 ) ;
}
return response () -> json ( $product ) ;
}
// Menyimpan produk baru
public function create_product ( Request $request)
{
$request -> validate ( [
'name' => 'required|string|max:255' ,
'price' => 'required|numeric' ,
'stock' => 'required|integer' ,
] ) ;
Product :: create ( $request -> all ()) ;
return response () -> json ( [ 'message' => 'Produk berhasil disimpan' ] , 201 ) ;
}
// Mengupdate produk berdasarkan ID
public function update_product ( Request $request , $id)
{
$product = Product :: find ( $id ) ;
if ( ! $product) {
return response () -> json ( [ 'message' => 'Produk tidak ditemukan' ] , 404 ) ;
}
$request -> validate ( [
'name' => 'required|string|max:255' ,
'price' => 'required|numeric' ,
'stock' => 'required|integer' ,
] ) ;
$product -> update ( $request -> all ()) ;
return response () -> json ( [ 'message' => 'Produk berhasil diupdate' ] , 200 ) ;
}
// Menghapus produk berdasarkan ID
public function delete_product ($id)
{
$product = Product :: find ( $id ) ;
if ( ! $product) {
return response () -> json ( [ 'message' => 'Produk tidak ditemukan' ] , 404 ) ;
}
$product -> delete () ;
return response () -> json ( [ 'message' => 'Produk berhasil dihapus' ] , 200 ) ;
}
}
Dalam contoh ini, paginate(10)
akan menampilkan 10 produk per halaman. Sesuaikan angka 10 dengan jumlah produk yang ingin Anda tampilkan per halaman.
Di dalam file products.index.blade.php
, tampilkan daftar produk dan tambahkan elemen pagination:
Copy @extends('layouts.app')
@section('content')
< h2 >Daftar Produk</ h2 >
< table class = "table" >
< thead >
< tr >
< th >Nama</ th >
< th >Harga</ th >
< th >Stok</ th >
< th >Aksi</ th >
</ tr >
</ thead >
< tbody >
@foreach ($products as $product)
< tr >
< td >{{ $product->name }}</ td >
< td >{{ $product->price }}</ td >
< td >{{ $product->stock }}</ td >
< td >
< a href = "{{ route('products.show', $product->id) }}" class = "btn btn-info" >Detail</ a >
< a href = "{{ route('products.edit', $product->id) }}" class = "btn btn-primary" >Edit</ a >
<form action="{{ route('products.destroy', $product->id) }}" method="POST" style="display:inline;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger" onclick="return confirm('Apakah Anda yakin ingin menghapus produk ini?')">Hapus</button>
</ form >
</ td >
</ tr >
@endforeach
</ tbody >
</ table >
<!-- Tampilkan navigasi pagination -->
{{ $products->links() }}
< a href = "{{ route('products.create') }}" class = "btn btn-success" >Tambah Produk Baru</ a >
@endsection
Pada bagian {{ $products->links() }}
, Laravel secara otomatis akan membuat tombol navigasi untuk Anda, memungkinkan pengguna untuk beralih antara halaman-halaman berikutnya dan sebelumnya.
Di bab berikutnya akan belajar cara membuat auth dengan laravel bootstrap Scaffolding di project yang telah dibuat.