调用未定义的方法 App\Models\Karyawan::links() - Laravel 8 中的分页
Call to undefined method App\Models\Karyawan::links() - Pagination in Laravel 8
我的控制器中有这段代码:
public function data_karyawan()
{
return view('data-karyawan', [
"title" => "Data Karyawan",
"karyawan" => Karyawan::with(['role', 'user'])->search(request(['search']))->paginate(10)
]);
}
但是当我尝试像这样在我的视图上分页时,出现错误:
{{ $karyawan->links() }}
我做错了什么?
Blade 文件:
@extends('layouts.main')
@section('container')
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-2 col-lg-2 d-md-block bg-light sidebar collapse">
<div class="position-sticky pt-3">
<div class="d-flex flex-column flex-shrink-0 p-3 bg-light" style="width: 200px;">
<svg class="bi me-2" width="40" height="0"></svg>
<span class="fs-10"><center><img src="/images/logo/selindo4.png" style="width:100px"></center></span>
</a>
<hr>
<ul class="nav nav-pills flex-column mb-auto">
<li class="nav-item">
<a href="/admin" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#home"/></svg>
Home
</a>
</li>
<li>
<a href="#" class="nav-link active" aria-current="page">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#people-circle"/></svg>
Data Karyawan
</a>
</li>
<li>
<a href="/admin/data-departemen" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#speedometer2"/></svg>
Departemen
</a>
</li>
<li>
<a href="/admin/data-cuti" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#table"/></svg>
Summary Cuti
</a>
</li>
</ul>
<hr>
<div class="dropdown">
<a href="#" class="d-flex align-items-center link-dark text-decoration-none dropdown-toggle" id="dropdownUser2" data-bs-toggle="dropdown" aria-expanded="false">
<img src="/images/avatar/avatar-2.png" alt="" width="32" height="40" class="rounded-circle me-2">
<strong>Admin</strong>
</a>
<ul class="dropdown-menu text-small shadow" aria-labelledby="dropdownUser2">
<li><a class="dropdown-item" href="#">Logout</a></li>
</ul>
</div>
</div>
</div>
</nav>
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2" style="position:absolute margin: auto auto"><p></p>Data Karyawan</h1>
</div>
<div class="row">
<div class="col-md-6">
<form action="/admin/data-karyawan">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Cari Karyawan" name = "search" value="{{ request('search') }}">
<button class="btn btn-outline-primary" type="submit">Search</button>
</div>
</form>
</div>
</div>
<center>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<hr>
<thead>
<tr>
<th>No.</th>
<th>NIK</th>
<th>Nama</th>
<th>JK</th>
<th>Tempat Lahir</th>
<th>Tanggal Lahir</th>
<th>Alamat</th>
<th>Agama</th>
<th>Jabatan</th>
<th>Divisi</th>
<th>Role</th>
<th>E-mail</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($karyawan as $karyawan) { ?>
<tr>
<td>{{ $i++ }}</td>
<td>{{ $karyawan["nik"] }}</td>
<td>{{ $karyawan["nama"] }}</td>
<td>{{ $karyawan["jk"] }}</td>
<td>{{ $karyawan["tempat_lahir"] }}</td>
<td>{{ $karyawan["tanggal_lahir"] }}</td>
<td>{{ $karyawan["alamat"] }}</td>
<td>{{ $karyawan["agama"] }}</td>
<td>{{ $karyawan["jabatan"] }}</td>
<td>{{ $karyawan["departemen"] }}</td>
<td>{{ $karyawan->role->nama_role }}</td>
<td>{{ $karyawan->user->email }}</td>
<td width = '180px'>
<a href="update.php?nik=<?php echo htmlspecialchars($karyawan['nik']); ?>" class="btn btn-warning" role="button">Update</a>
<a href="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?nik=<?php echo $karyawan['nik']; ?>" class="btn btn-danger" role="button">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
<table>
<tr> <td><a href="/admin/create" class="btn btn-primary" role="button">Tambah Data Karyawan</a></td>
<td width='100px'> </td><td width='100px'> </td><td width='100px'> </td>
<td width='100px'> </td><td width='100px'> </td><td width='100px'> </td>
<td width='100px'> </td><td width='100px'> </td><td width='25px'> </td>
</table>
</div>
</main>
</div>
</div>
{{ $karyawan->links() }}
@endsection
非常感谢。
这里有一个问题:
$karyawan as $karyawan
您覆盖了值
在您的 blade 文件中,您对 foreach 语句使用相同的变量。因此压倒一切。
将 foreach ($karyawan as $karyawan)
更改为类似 foreach ($karyawan as $kar)
的内容,然后将下面块中出现的所有 $karyawan
替换为 $kar
<td>{{ $karyawan["nik"] }}</td>
<td>{{ $karyawan["nama"] }}</td>
<td>{{ $karyawan["jk"] }}</td>
<td>{{ $karyawan["tempat_lahir"] }}</td>
<td>{{ $karyawan["tanggal_lahir"] }}</td>
<td>{{ $karyawan["alamat"] }}</td>
<td>{{ $karyawan["agama"] }}</td>
<td>{{ $karyawan["jabatan"] }}</td>
<td>{{ $karyawan["departemen"] }}</td>
<td>{{ $karyawan->role->nama_role }}</td>
<td>{{ $karyawan->user->email }}</td>
<td width = '180px'>
<a href="update.php?nik=<?php echo htmlspecialchars($karyawan['nik']); ?>" class="btn btn-warning" role="button">Update</a>
<a href="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?nik=<?php echo $karyawan['nik']; ?>" class="btn btn-danger" role="button">Delete</a>
</td>
我的控制器中有这段代码:
public function data_karyawan()
{
return view('data-karyawan', [
"title" => "Data Karyawan",
"karyawan" => Karyawan::with(['role', 'user'])->search(request(['search']))->paginate(10)
]);
}
但是当我尝试像这样在我的视图上分页时,出现错误:
{{ $karyawan->links() }}
我做错了什么?
Blade 文件:
@extends('layouts.main')
@section('container')
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-2 col-lg-2 d-md-block bg-light sidebar collapse">
<div class="position-sticky pt-3">
<div class="d-flex flex-column flex-shrink-0 p-3 bg-light" style="width: 200px;">
<svg class="bi me-2" width="40" height="0"></svg>
<span class="fs-10"><center><img src="/images/logo/selindo4.png" style="width:100px"></center></span>
</a>
<hr>
<ul class="nav nav-pills flex-column mb-auto">
<li class="nav-item">
<a href="/admin" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#home"/></svg>
Home
</a>
</li>
<li>
<a href="#" class="nav-link active" aria-current="page">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#people-circle"/></svg>
Data Karyawan
</a>
</li>
<li>
<a href="/admin/data-departemen" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#speedometer2"/></svg>
Departemen
</a>
</li>
<li>
<a href="/admin/data-cuti" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#table"/></svg>
Summary Cuti
</a>
</li>
</ul>
<hr>
<div class="dropdown">
<a href="#" class="d-flex align-items-center link-dark text-decoration-none dropdown-toggle" id="dropdownUser2" data-bs-toggle="dropdown" aria-expanded="false">
<img src="/images/avatar/avatar-2.png" alt="" width="32" height="40" class="rounded-circle me-2">
<strong>Admin</strong>
</a>
<ul class="dropdown-menu text-small shadow" aria-labelledby="dropdownUser2">
<li><a class="dropdown-item" href="#">Logout</a></li>
</ul>
</div>
</div>
</div>
</nav>
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2" style="position:absolute margin: auto auto"><p></p>Data Karyawan</h1>
</div>
<div class="row">
<div class="col-md-6">
<form action="/admin/data-karyawan">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Cari Karyawan" name = "search" value="{{ request('search') }}">
<button class="btn btn-outline-primary" type="submit">Search</button>
</div>
</form>
</div>
</div>
<center>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<hr>
<thead>
<tr>
<th>No.</th>
<th>NIK</th>
<th>Nama</th>
<th>JK</th>
<th>Tempat Lahir</th>
<th>Tanggal Lahir</th>
<th>Alamat</th>
<th>Agama</th>
<th>Jabatan</th>
<th>Divisi</th>
<th>Role</th>
<th>E-mail</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($karyawan as $karyawan) { ?>
<tr>
<td>{{ $i++ }}</td>
<td>{{ $karyawan["nik"] }}</td>
<td>{{ $karyawan["nama"] }}</td>
<td>{{ $karyawan["jk"] }}</td>
<td>{{ $karyawan["tempat_lahir"] }}</td>
<td>{{ $karyawan["tanggal_lahir"] }}</td>
<td>{{ $karyawan["alamat"] }}</td>
<td>{{ $karyawan["agama"] }}</td>
<td>{{ $karyawan["jabatan"] }}</td>
<td>{{ $karyawan["departemen"] }}</td>
<td>{{ $karyawan->role->nama_role }}</td>
<td>{{ $karyawan->user->email }}</td>
<td width = '180px'>
<a href="update.php?nik=<?php echo htmlspecialchars($karyawan['nik']); ?>" class="btn btn-warning" role="button">Update</a>
<a href="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?nik=<?php echo $karyawan['nik']; ?>" class="btn btn-danger" role="button">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
<table>
<tr> <td><a href="/admin/create" class="btn btn-primary" role="button">Tambah Data Karyawan</a></td>
<td width='100px'> </td><td width='100px'> </td><td width='100px'> </td>
<td width='100px'> </td><td width='100px'> </td><td width='100px'> </td>
<td width='100px'> </td><td width='100px'> </td><td width='25px'> </td>
</table>
</div>
</main>
</div>
</div>
{{ $karyawan->links() }}
@endsection
非常感谢。
这里有一个问题:
$karyawan as $karyawan
您覆盖了值
在您的 blade 文件中,您对 foreach 语句使用相同的变量。因此压倒一切。
将 foreach ($karyawan as $karyawan)
更改为类似 foreach ($karyawan as $kar)
的内容,然后将下面块中出现的所有 $karyawan
替换为 $kar
<td>{{ $karyawan["nik"] }}</td>
<td>{{ $karyawan["nama"] }}</td>
<td>{{ $karyawan["jk"] }}</td>
<td>{{ $karyawan["tempat_lahir"] }}</td>
<td>{{ $karyawan["tanggal_lahir"] }}</td>
<td>{{ $karyawan["alamat"] }}</td>
<td>{{ $karyawan["agama"] }}</td>
<td>{{ $karyawan["jabatan"] }}</td>
<td>{{ $karyawan["departemen"] }}</td>
<td>{{ $karyawan->role->nama_role }}</td>
<td>{{ $karyawan->user->email }}</td>
<td width = '180px'>
<a href="update.php?nik=<?php echo htmlspecialchars($karyawan['nik']); ?>" class="btn btn-warning" role="button">Update</a>
<a href="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?nik=<?php echo $karyawan['nik']; ?>" class="btn btn-danger" role="button">Delete</a>
</td>