Menampilkan fakultas dan program studi sesuai dengan fakultas

saya ada permasalahan dalam menampikan list fakultas dan program studi sesuai dengan fakultasnya berdasarkan ID universitas yg dipilih dalam bentuk rest API. list fakultasnya sudah bisa tapi pada program studinya tidak sesuai. berikut code saya

ini struktur tbl_major

ini struktur tabel fakultas

controller

public function list_faculty($id_university){
  $data_fakul = [];
  $progdi = array();
  $cek = array();

$fakul =  Faculty_model::where('campus_id', $id_university)->get();
foreach ($fakul as $key =>  $values) {
    $data_fakul[$key]['faculty'] = $values;
    $major =  Major_model::where('faculty_id', $values->faculty_id)->get();
        foreach ($major as $key2 => $value) {
        $progdi[$key2] = $value;
        $data_fakul[$key]['faculty']['progdi'] = $progdi;
    }
    }
return response()->json([
    "response_code"=>"00",
    "desc"=>"success",
    "list_faculty" =>$data_fakul,
]);

}

outputnya

{
    "response_code": "00",
    "desc": "success",
    "list_faculty": [
        {
            "faculty": {
                "faculty_id": 27,
                "faculty_name": "Fakultas Teknologi Informasi",
                "campus_id": "ID50711",
                "progdi": [
                    {
                        "major_id": 1,
                        "faculty_id": 27,
                        "major_name": "S1 Teknik Informatika",
                        "major_accred": "A",
                        "entry_fee": 19500000,
                        "credit_fee": 225000,
                        "bpp_fee": 3000000,
                        "re_registration_fee": 6638000,
                        "major_link": null,
                        "category": null
                    },
                    {
                        "major_id": 4,
                        "faculty_id": 27,
                        "major_name": "S1 Sistem Informasi",
                        "major_accred": "A",
                        "entry_fee": 19500000,
                        "credit_fee": 225000,
                        "bpp_fee": 3000000,
                        "re_registration_fee": 225000,
                        "major_link": null,
                        "category": null
                    },
                    {
                        "major_id": 5,
                        "faculty_id": 27,
                        "major_name": "D3 Teknik Informatika",
                        "major_accred": "B",
                        "entry_fee": 8000000,
                        "credit_fee": 160000,
                        "bpp_fee": 1200000,
                        "re_registration_fee": 4210000,
                        "major_link": null,
                        "category": null
                    }
                ]
            }
        },
        {
            "faculty": {
                "faculty_id": 28,
                "faculty_name": "Fakultas Psikologi",
                "campus_id": "ID50711",
                "progdi": [
                    {
                        "major_id": 2,
                        "faculty_id": 28,
                        "major_name": "S1 Psikologi",
                        "major_accred": "B",
                        "entry_fee": 17500000,
                        "credit_fee": 200000,
                        "bpp_fee": 2400000,
                        "re_registration_fee": 6038000,
                        "major_link": null,
                        "category": null
                    },
                    {
                        "major_id": 4,
                        "faculty_id": 27,
                        "major_name": "S1 Sistem Informasi",
                        "major_accred": "A",
                        "entry_fee": 19500000,
                        "credit_fee": 225000,
                        "bpp_fee": 3000000,
                        "re_registration_fee": 225000,
                        "major_link": null,
                        "category": null
                    },
                    {
                        "major_id": 5,
                        "faculty_id": 27,
                        "major_name": "D3 Teknik Informatika",
                        "major_accred": "B",
                        "entry_fee": 8000000,
                        "credit_fee": 160000,
                        "bpp_fee": 1200000,
                        "re_registration_fee": 4210000,
                        "major_link": null,
                        "category": null
                    }
                ]
            }
        },
        {
            "faculty": {
                "faculty_id": 29,
                "faculty_name": "Fakultas Teologi",
                "campus_id": "ID50711",
                "progdi": [
                    {
                        "major_id": 3,
                        "faculty_id": 29,
                        "major_name": "S1 Teologi",
                        "major_accred": "A",
                        "entry_fee": 13500000,
                        "credit_fee": 175000,
                        "bpp_fee": 2400000,
                        "re_registration_fee": 5338000,
                        "major_link": null,
                        "category": null
                    },
                    {
                        "major_id": 4,
                        "faculty_id": 27,
                        "major_name": "S1 Sistem Informasi",
                        "major_accred": "A",
                        "entry_fee": 19500000,
                        "credit_fee": 225000,
                        "bpp_fee": 3000000,
                        "re_registration_fee": 225000,
                        "major_link": null,
                        "category": null
                    },
                    {
                        "major_id": 5,
                        "faculty_id": 27,
                        "major_name": "D3 Teknik Informatika",
                        "major_accred": "B",
                        "entry_fee": 8000000,
                        "credit_fee": 160000,
                        "bpp_fee": 1200000,
                        "re_registration_fee": 4210000,
                        "major_link": null,
                        "category": null
                    }
                ]
            }
        },
        {
            "faculty": {
                "faculty_id": 30,
                "faculty_name": "Fakultas Ekonomika dan Bisnis",
                "campus_id": "ID50711",
                "progdi": [
                    {
                        "major_id": 6,
                        "faculty_id": 30,
                        "major_name": "S1 Akuntansi",
                        "major_accred": "A",
                        "entry_fee": 0,
                        "credit_fee": 6500000,
                        "bpp_fee": 0,
                        "re_registration_fee": 0,
                        "major_link": null,
                        "category": null
                    },
                    {
                        "major_id": 4,
                        "faculty_id": 27,
                        "major_name": "S1 Sistem Informasi",
                        "major_accred": "A",
                        "entry_fee": 19500000,
                        "credit_fee": 225000,
                        "bpp_fee": 3000000,
                        "re_registration_fee": 225000,
                        "major_link": null,
                        "category": null
                    },
                    {
                        "major_id": 5,
                        "faculty_id": 27,
                        "major_name": "D3 Teknik Informatika",
                        "major_accred": "B",
                        "entry_fee": 8000000,
                        "credit_fee": 160000,
                        "bpp_fee": 1200000,
                        "re_registration_fee": 4210000,
                        "major_link": null,
                        "category": null
                    }
                ]
            }
        }
    ]
}

response diatas masih menampikan data progdi tidak sesuai dengan kode fakultas misalnya progdi dengan kode fakultas 27 masih muncul di progdi selain kode fakultas tersebut atau masih berulang. mohon bantuannya

avatar niko069
@niko069

48 Kontribusi 11 Poin

Diperbarui 4 tahun yang lalu

1 Jawaban:

Coba perbaiki dulu syntax sqlnya dan cobain ditambahain GROUP BY faculty_id

avatar PanduMA
@PanduMA

1 Kontribusi 0 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban