1

I tried filtering data based on the 'Jadwal_kuliah' and 'khs' tables but I found an error "The object of the class stdClass could not be converted to string" :

function jadwal_kuliah_json()
{
    $nim = Auth::guard('mahasiswa')->user()->nim;

    $tahun_akademik = \DB::table('tahun_akademik')->where('status', 'y')->first();

    $kelas = \DB::table('khs')
        ->select('kode_kelas')
        ->where('nim', $nim)
        ->get();

    $jadwal = \DB::table('jadwal_kuliah')
        ->join('khs', 'jadwal_kuliah.kode_mk', '=', 'khs.kode_mk')
        ->where('jadwal_kuliah.kode_kelas', $kelas)
        ->where('khs.nim', $nim)
        ->select('matakuliah.nama_mk', 'jadwal_kuliah.hari', 'jam_kuliah.jam', 'ruangan.nama_ruangan', 'dosen.nama', 'kelas.nama_kelas')
        ->get();

    return Datatables::of($jadwal)->make(true);
}

JSON

{
    "id": 36,
    "kode_kelas": "TI18A",
    "hari": "senin",
    "kode_mk": "TI001",
    "nidn": "15110185",
    "jam": "1",
    "kode_jurusan": "ifd3",
    "kode_ruangan": "LBKom",
    "kode_tahun_akademik": "20182",
    "semester": 1,
    "created_at": "2019-02-28 15:02:52",
    "updated_at": "2019-02-28 15:02:52"
}

What needs to be changed?

  • Can you share the full and exact error message, and the line causing it? – Nico Haase Apr 11 at 14:18
  • $kelas is an object, it must be changed to a string, but if the class is changed to first() then only 1 data is displayed – Khaiz Badaru Tammam Apr 11 at 14:35
  • Well, then "change" it to a string if you think that is the way to go. I'd recommend to use some kind of ID or whatever you store in the database to join these tables - but still, what's the problem in doing it? – Nico Haase Apr 11 at 14:53
  • I don't know what queries to use to filter data. if you use a condition where it must call one of the other conditions – Khaiz Badaru Tammam Apr 11 at 15:20
  • What do you mean by "call one of the other conditions"? Why not use a scalar value like an ID? As we don't know anything about the structure of your models and tables, it's hard to help – Nico Haase Apr 11 at 15:22
1

$kelas is a collection and you're passing it as a string condition in :

->where('jadwal_kuliah.kode_kelas',$kelas)

First return an object using first() like :

$kelas = \DB::table('khs')
          ->select('kode_kelas')
          ->where('nim',$nim)
          ->first();

Then you must get the property instead like :

->where('jadwal_kuliah.kode_kelas',$kelas->kode_kelas)
  • how to change to string? – Khaiz Badaru Tammam Apr 11 at 14:18
  • 1
    Check my update – Zakaria Acharki Apr 11 at 14:19
  • I have tried and an error appears "Property [kode_kelas] does not exist on this collection instance." because $kelas must use first(). I tried first() but only display 1 data – Khaiz Badaru Tammam Apr 11 at 14:22
  • Yes, it will return one object, else if you want a collection then how you'll handle multiple kode_kelas in ->where('jadwal_kuliah.kode_kelas', multiple_kode_kelas ) ? – Zakaria Acharki Apr 11 at 14:23
  • what do you mean? Please show me for example? :D – Khaiz Badaru Tammam Apr 11 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.