无法在Laravel中将类stdClass的对象转换为字符串?

时间:2019-04-11 14:11:06

标签: arrays laravel query-builder

我尝试根据'Jadwal_kuliah'和'khs'表过滤数据,但发现错误“类stdClass的对象无法转换为字符串”:

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"
}

需要更改什么?

1 个答案:

答案 0 :(得分:1)

$kelas是一个集合,您将其作为字符串条件传递给:

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

首先使用first()返回一个对象,如:

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

然后,您必须像这样获取属性:

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