Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Call to undefined method App\Models\Karyawan::links() – Pagination in Laravel 8

I have this code in my controller:

public function data_karyawan()
    {
        return view('data-karyawan', [
            "title" => "Data Karyawan",
            "karyawan" => Karyawan::with(['role', 'user'])->search(request(['search']))->paginate(10)
        ]);
    }

but when I tried to give pagination on my view like this, there is an error:

{{ $karyawan->links() }}

what did I do wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Blade file:

@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'>&nbsp;</td><td width='100px'>&nbsp;</td><td width='100px'>&nbsp;</td>
              <td width='100px'>&nbsp;</td><td width='100px'>&nbsp;</td><td width='100px'>&nbsp;</td>
              <td width='100px'>&nbsp;</td><td width='100px'>&nbsp;</td><td width='25px'>&nbsp;</td>
            </table>
      </div>
    </main>
  </div>
</div>

{{ $karyawan->links() }}

@endsection

thank you very much.

>Solution :

There is an issue here :

$karyawan as $karyawan

you override the value

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading