Using pagination with group by in Livewire

75 Views Asked by At

I have a problem. I have a code to retrieve all the data with group by that goes like this.

public function getAllDokumen()
    {
        $dokumen = Dokumen::get()->groupBy('debitur_id')->all();
        return $dokumen;
    }

And then, I need to display it like this

@foreach ($allDokumen as $dok)
                    <tr>
                        <td rowspan="11">{{ $loop->iteration }}</td>
                        <td rowspan="11">{{ $dok->first()->debitur->no_debitur }}</td>
                        <td rowspan="11">{{ $dok->first()->debitur->nama_debitur }}</td>
                    </tr>
                    @foreach (['PPJB', 'AJB', 'SKMHT', 'APHT', 'PH', 'SHT', 'IMB', 'Sertipikat', 'PK', 'CN'] as $jenis)
                        @php
                            $d = $dok->where('jenis', $jenis)->first();
                        @endphp
                        @if ($d)
                            <tr>
                                <td>
                                    {{ $jenis }}
                                </td>
                                <td>
                                    @if ($d->status_pinjaman == 0)
                                        Tersedia
                                    @elseif ($d->status_keluar == 1)
                                        Keluar
                                    @elseif ($d->status_pinjaman == 1)
                                        Dipinjam
                                    @endif
                                </td>
                            </tr>
                        @else
                            <tr>
                                <td>{{ $jenis }}</td>
                                <td>Tidak Tersedia</td>
                            </tr>
                        @endif
                    @endforeach
                @endforeach

<div class="mt-4 items-center">
        {{ $allDokumen->links() }}
    </div>

Is there any way to paginate the result from indexDokumen()

I have tried to just using paginate(), but the result isn't same as I want. I tried using paginate()->groupBy('debitur_id'), but I can't use the links() method of pagination.

1

There are 1 best solutions below

1
Akshay ak On
public function getAllDokumen()
{    $currentPage = request()->get('page', 1);
    $dokumen = Dokumen::with('debitur')
               ->selectRaw('*, COUNT(*) as total')
               ->groupBy('debitur_id')
               ->paginate(10, ['*'], 'page', $currentPage);
    return $dokumen;
}