mardi 23 juillet 2019

Get some records each

(first, im sorry for my bad english) i have DETAILTUGAS collection with 2 records . It hasMany with DATASOAL records. i need help how to get example: randomed 2 DATASOAL records for each id_kd from DETAILTUGAS

current DETAILTUGAS records:

 "id": 5,
    "id_tugas": 7,
    "id_kd": 2,
    "jumlah_soal": 2,
    "created_at": "2019-07-22 15:38:42",
    "updated_at": "2019-07-22 15:38:42",
    "tugas": {
      "id": 7,
      "jenis_tugas": "Tugas Harian",
      "mulai": "2011-08-19 13:45:00",
      "berakhir": "2011-08-20 13:45:00",
      "waktu": "01:00:00",
      "id_kelas": 1,
      "id_guru": 3,
      "id_mapel": 4,
      "password": null,
      "created_at": "2019-07-22 15:38:42",
      "updated_at": "2019-07-22 15:38:42"
    }
  },
  {
    "id": 6,
    "id_tugas": 7,
    "id_kd": 3,
    "jumlah_soal": 2,
    "created_at": "2019-07-22 15:38:42",
    "updated_at": "2019-07-22 15:38:42",
    "tugas": {
      "id": 7,
      "jenis_tugas": "Tugas Harian",
      "mulai": "2011-08-19 13:45:00",
      "berakhir": "2011-08-20 13:45:00",
      "waktu": "01:00:00",
      "id_kelas": 1,
      "id_guru": 3,
      "id_mapel": 4,
      "password": null,
      "created_at": "2019-07-22 15:38:42",
      "updated_at": "2019-07-22 15:38:42"
    }

and there is my current code:

$detail_tugas = DetailTugas::with('tugas')->where('id_tugas',$id_tugas)->get();
        foreach ($detail_tugas as $key => $dt) {
            $ikd[] = $dt->id_kd;
            $js  = $js + $dt->jumlah_soal;
            $s = DataSoal::where('id_mapel',$dt->tugas->id_mapel)
                        ->where('id_kd',$dt->id_kd)
                        ->orderByRaw('RAND()')
                        ->take($dt->jumlah_soal)
                        ->get();
        }
        // return $soal;
        return $s;



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire