skip to Main Content

I have tried everything to get the data total of Jumlah Hadir, Jumlah Sakit, and Jumlah Izin there for every user. as a beginner I am very confused. For a clear view I want, you can see the image below:

Tabel View Image

table structure in database

this is a piece of my queries in controller, so many query from anywhere I try to get the data total, and this is one of many:

$data = Absence::with('teacher','data_mapel','data_kelas','data_siswa','data_tahun_ajaran','data_unit') ->select('*', DB::raw('(SELECT COUNT(*) FROM absensi_siswa WHERE absensi = "Hadir") as jumlah_hadir'));

with the query above. I get the result like the table above.

I want the data unique based on Nama Lengkap field and I wish the data total show up on my table for every user.

what queries should I write on to get the query works? thanks in advance.

2

Answers


  1. try using groupBy('siswa_id') on your query builder and see check the result

    Login or Signup to reply.
  2. Try using count i believe, if the absence was another model or table and Nama Lengkap is came from the User, try use this:

    $user = User::with('absence')->first();
    $absence = [
       'id' => $user->id,
       'nisn' => $user->nisn,
       'nama' => $user->name,
       'absence' => [
            'hadir' => $user->absence()->where('absensi', 'Hadir')->count()
            'ijin' => $user->absence()->where('absensi', 'Ijin')->count()
        ]
    ];
    

    At the end of the day, this will lead with a new collection, you can find out how collection works in Laravel. And for looping, you can try using mapping or foreach loop for each of the data and use those array as an example. Another approach you can also use withCount but it will take another level if you want to filter out the results.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search