Home > front end >  Call to undefined method App\Models\Karyawan::links() - Pagination in Laravel 8
Call to undefined method App\Models\Karyawan::links() - Pagination in Laravel 8

Time:04-29

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?

Blade file:

@extends('layouts.main')

@section('container')    
<div >
  <div >
   
    <nav id="sidebarMenu" >
        <div >
          <div  style="width: 200px;">
              <svg  width="40" height="0"></svg>
              <span ><center><img src="/images/logo/selindo4.png" style="width:100px"></center></span>
            </a>
            <hr>
            <ul >
              <li >
                <a href="/admin" >
                  <svg  width="16" height="40"><use xlink:href="#home"/></svg>
                  Home
                </a>
              </li>
              <li>
                <a href="#"  aria-current="page">
                  <svg  width="16" height="40"><use xlink:href="#people-circle"/></svg>
                  Data Karyawan
                </a>
              </li>
              <li>
                <a href="/admin/data-departemen" >
                  <svg  width="16" height="40"><use xlink:href="#speedometer2"/></svg>
                  Departemen
                </a>
              </li>
              <li>
                <a href="/admin/data-cuti" >
                  <svg  width="16" height="40"><use xlink:href="#table"/></svg>
                  Summary Cuti
                </a>
              </li>
            </ul>
            <hr>
            <div >
              <a href="#"  id="dropdownUser2" data-bs-toggle="dropdown" aria-expanded="false">
                <img src="/images/avatar/avatar-2.png" alt="" width="32" height="40" >
                <strong>Admin</strong>
              </a>
              <ul  aria-labelledby="dropdownUser2">
                <li><a  href="#">Logout</a></li>
              </ul>
            </div>
          </div>
        </div>
      </nav>

    <main >
      <div >
          <h1  style="position:absolute margin: auto auto"><p></p>Data Karyawan</h1>
        </div>
      
        <div >
          <div >
            <form action="/admin/data-karyawan">
              <div >
                <input type="text"  placeholder="Cari Karyawan" name = "search" value="{{ request('search') }}">
                <button  type="submit">Search</button>
              </div>
            </form>
          </div>
        </div>

        <center>
          <table  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']); ?>"  role="button">Update</a>
                  <a href="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?nik=<?php echo $karyawan['nik']; ?>"  role="button">Delete</a>
                  </td>
              </tr>
              <?php } ?>
            </tbody>
          </table>
        </center>
        <table>
          <tr>  <td><a href="/admin/create"  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.

CodePudding user response:

There is an issue here :

$karyawan as $karyawan

you override the value

CodePudding user response:

In your blade file, you use the same variables for your foreach statement. Thefore overriding one.

Change foreach ($karyawan as $karyawan) to something like foreach ($karyawan as $kar) and then replace all occurances of $karyawan in the below block with $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']); ?>"  role="button">Update</a>
                  <a href="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?nik=<?php echo $karyawan['nik']; ?>"  role="button">Delete</a>
                  </td>
  • Related