batotanete's avatar

Display PAGINATION groupBy in BLADE View

How to display PAGINATION in BLADE View, please?

I have tried using:

{{ $monitoring_kgb->links() }}

But an error occurred:

Method Illuminate\Database\Eloquent\Collection::links does not exist.

Controller:

        $monitoring_kgb = MonitoringKGB::with('pegawai')
        ->orderBy('tgl_mulai_kgb', 'ASC')
        ->paginate(10)
        ->groupBy(function ($val) {
          return Carbon::parse($val->tgl_mulai_kgb)->format('M');
          });

dd($monitoring_kgb);

Illuminate\Database\Eloquent\Collection {#472 ▼
  #items: array:5 [▼
    "Jan" => Illuminate\Database\Eloquent\Collection {#471 ▶}
    "Feb" => Illuminate\Database\Eloquent\Collection {#470 ▶}
    "Mar" => Illuminate\Database\Eloquent\Collection {#469 ▶}
    "May" => Illuminate\Database\Eloquent\Collection {#468 ▶}
    "Jun" => Illuminate\Database\Eloquent\Collection {#467 ▼
      #items: array:1 [▼
        0 => App\Models\MonitoringKGB {#505 ▼
          #table: "monitoring_k_g_b_s"
          #fillable: array:4 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:8 [▼
            "id" => 8
            "tgl_mulai_kgb" => "2021-06-16"
            "pegawais_id" => 1
            "tindakan" => "Tunda"
            "keterangan" => "f your parent model does not use id as its primary key, or you wish to find the associated mod"
            "deleted_at" => null
            "created_at" => null
            "updated_at" => null
          ]
          #original: array:8 [▶]
          #changes: []
          #casts: array:1 [▶]
          #classCastCache: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: array:1 [▼
            "pegawai" => App\Models\Pegawai {#515 ▼
              #table: "pegawais"
              #fillable: array:12 [▶]
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:16 [▼
                "id" => 1
                "nm_depan" => "Markus"
                "nm_belakang" => "Lukas"
                "t4_lahir" => "sdfsdf"
                "tgl_lahir" => "2021-03-10"
                "nip" => "234234234234234234"
                "jabatan" => "CPNS"
                "tlp_hp" => "234234234234"
                "pangkat_gol" => "Penata/ III/c"
                "gelar_blkng" => "MT"
                "photo" => "528903824.jpg"
                "user_id" => 2
                "hak_akses" => null
                "deleted_at" => null
                "created_at" => "2021-03-21 00:03:02"
                "updated_at" => "2021-03-21 19:56:42"
              ]
              #original: array:16 [▶]
              #changes: []
              #casts: array:1 [▶]
              #classCastCache: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
              #forceDeleting: false
              -roleClass: null
              -permissionClass: null
            }
          ]
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
          #forceDeleting: false
        }
      ]
    }
  ]
}

View:

        @if(count($monitoring_kgb)>0)
        @foreach ($monitoring_kgb as $bulan => $daftar)
          <tr class="group">
            <td colspan="6">
              {{date('F Y', strtotime($bulan))}}
            </td>
          </tr>
          <tr>
          @foreach ($daftar as $item)
            <tr>
              <td>
                <div class="dropdown">
                  <span class="bx bx-dots-vertical-rounded font-medium-3 dropdown-toggle nav-hide-arrow cursor-pointer"
                    data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" role="menu"></span>
                  <div class="dropdown-menu dropdown-menu-right">
                    <a class="dropdown-item" href="javascript:void(0);"><i class="bx bx-edit-alt mr-1"></i> edit</a>
                    <a class="dropdown-item" href="javascript:void(0);"><i class="bx bx-trash mr-1"></i> delete</a>
                  </div>
                </div>
              </td>
              <td>{{ $item->pegawai->nip }}</td>
              <td>{{ $item->pegawai->nm_depan }} {{ $item->pegawai->nm_belakang }}</td>
              <td>{{ $item->pegawai->pangkat_gol }}</td>
              <td>{{ $item->tindakan }}</td>
              <td>{{ $item->keterangan }}</td>
            </tr>
          @endforeach
        @endforeach
      @else
          <div class="alert border-warning alert-dismissible mt-0 mb-0 mr-2 ml-2" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
            <div class="d-flex align-items-center">
              <i class="bx bx-error-circle"></i>
              <span>
                Data Monitoring Kenaikan Gaji Berkala tidak tersedia !!
              </span>
            </div>
          </div>
      @endif
        </tbody>
      </table>
      <!-- table ends -->
    </div>
  </div>
  <div class="pagination pagination-success justify-content-center">
    {{-- {{ $monitoring_kgb->fragment('tgl_mulai_kgb')->links() }} --}}
    {{-- {{ $monitoring_kgb->links() }} --}}
    {{-- {{ $monitoring_kgb->links() }} --}}
    {{-- {{ $monitoring_kgb->withQueryString()->links() }} --}}
    {{-- {{ $monitoring_kgb->render() }} --}}
    {{-- {{ $monitoring_kgb->withQueryString()->links() }} --}}
    zxczxczxc
    {{-- {!! $monitoring_kgb->render() !!} --}}
  </div>
0 likes
7 replies
MostafaGamal's avatar

groupBy not working with pagination according to laravel 7 documentation

Note: Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.

https://laravel.com/docs/7.x/pagination

1 like
batotanete's avatar

Ohh good, thanks for the information because I have trouble finding a reference tutorial about this.

jlrdw's avatar

Use a length aware paginator.

Also this

{{-- {!! $monitoring_kgb->render() !!} --}}

should be

{{ $monitoring_kgb->links() }}

I don't even know what {{-- {!! $monitoring_kgb->render() !!} --}} is.

1 like
jlrdw's avatar

So you have your pagination problem solved.

batotanete's avatar

Yes, I need to find a table template related to this. Thank you for the respons.

Please or to participate in this conversation.