Home > Back-end >  Problem in showing multiple data count results in one page with different condition in livewire
Problem in showing multiple data count results in one page with different condition in livewire

Time:04-21

I'm using livewire and I want to display some data count results based on their respective conditions..

However, the view page only shows 1 result of count, which should be 4. Here's the result..

And here's the line of code in the livewire render component:

$prospek = Prospek::orderby('prospek.id', 'desc');
     
        if($this->status == 'baru')
        {
            $prospek = $prospek->where('status_donor', $this->status);
            $data ['ket'] = "Prospek";
        } 
        
        if($this->status == 'donatur')
        {
            $prospek = $prospek->where('status_donor', $this->status);
            $data ['ket'] = "Donatur";
        } 
        
        if($this->status == 'nonaktif')
        {
            $prospek = $prospek->where('status_donor', $this->status);
            $data ['ket'] = "Non Aktif";
        }
        
        if($this->status == 'hapus')
        {
            $prospek = $prospek->where('status_donor', $this->status);
            $data ['ket'] = "Dihapus";
        }

        if($this->cariNama != null ){
            $prospek = $prospek->where('nama','LIKE', '%'.$this->cariNama.'%');
        }
        
        if($this->cariHp != null ){
            $prospek = $prospek->where('hp', 'LIKE', '%'.$this->cariHp.'%');
        }

        $data ['prospek'] = $prospek->paginate($this->jumlahBaris);

        $data ['jmlBaru'] = $prospek->where('status_donor', 'baru')->count();
        $data ['jmlDonatur'] = $prospek->where('status_donor', 'donatur')->count();
        $data ['jmlNonAktif'] = $prospek->where('status_donor', 'nonaktif')->count();
        $data ['jmlHapus'] = $prospek->where('status_donor', 'hapus')->count();

        return view('livewire.donatur.tabel-donatur', $data);

And here's the code line in blade file:

<button type="button" data-dismiss="modal" wire:click="status(0)" data-toggle="tab"
        href="#donatur">
        Prospek {{ number_format($jmlBaru, 0, ',', '.') }}</button>
    <button type="button" data-dismiss="modal" wire:click="status(1)" data-toggle="tab"
        href="#donatur">
        Donatur {{ number_format($jmlDonatur, 0, ',', '.') }}</button>
    <button type="button" data-dismiss="modal" wire:click="status(2)" data-toggle="tab"
        href="#donatur">
        Non Aktif {{ number_format($jmlNonAktif, 0, ',', '.') }}</button>
    <button type="button" data-dismiss="modal" wire:click="status(3)" data-toggle="tab"
        href="#donatur">
        Dihapus {{ number_format($jmlHapus, 0, ',', '.') }}</button>

Can anyone show me how it should be?

CodePudding user response:

This is not related to Livewire but purely to Laravel and the way you're utilizing the query builder.

On your first line, you set the query: $prospek = Prospek::orderby('prospek.id', 'desc');. Then, with each if statement, you add conditions to the query. Then, at last, you want to get the count of each different status.

However, you're adding conditions to the query before you fetch the count. If you were to dump the query (\DB::enableQueryLog() before starting your query, dd(\DB::getQueryLog()) after finishing the last query) you would see that each condition has been added. Basically, your jmlHapus will be a Prospek that has to have each of the 4 queried statuses (which will never happen if I see your code).

Instead, what you can do is the following:

$counts = $prospek->selectRaw('status_donor, COUNT(*) as count')
    ->groupBy('status_donor')
    ->pluck('count', 'status_donor');

Now $counts will be a collection where each key is the status and the values are the counts.

Then, you can simply set your values like so:

$data['jmlBaru'] = $counts['baru'] ?? 0;
$data['jmlDonatur'] = $counts['donatur'] ?? 0;
$data['jmlNonAktif'] = $counts['nonaktif'] ?? 0;
$data['jmlHapus'] = $counts['hapus'] ?? 0;

I personally would suggest making this more dynamic, but for now this should get your counts working.

  • Related