我是laravel的新手,我遇到了这个问题,我无法从关系表中获取所有数据,
这是我的 Krs.php 模型:
protected $fillable = ['nim','nip','kode_mk','absen','uts','uas'];
protected $table = 'krs';
public function mahasiswa(){
return $this->hasMany('App\Mahasiswa');
}
public function dosen(){
return $this->hasMany('App\Dosen');
}
public function makul(){
return $this->hasMany('App\Matakuliah');
}
这是我的 Matakuliah.php 模型:
protected $fillable = ['kode_mk','makul','sks'];
protected $table = 'mata_kuliah';
public function krs(){
return $this->belongsTo('\App\Krs');
}
这是我的 Dosen.php 模型:
protected $fillable =['nip','nama','jeniskelamin','alamat','notlp'];
protected $table='dosen';
public function krs(){
return $this->belongsTo('App\Krs');
}
这是我的 Mahasiswa.php 模型:
protected $fillable = ['nim','nama','alamat','jenis_kelamin','no_tlp','email','tempat','tanggal','link','id_jurusan'];
protected $table = 'mahasiswa';
public function jurusan(){
return $this->hasOne('App\Jurusan');
}
public function krs(){
return $this->belongsTo('App\Krs');
}
这是我的 KrsController.php :
public function index()
{
$data = Krs::with(['mahasiswa','dosen','makul'])->first()->toArray();
return view('Krs.krsIndex',compact('data'));
}
如何从所有表中获取所有数据?例如,我想从mahasiswa获得 nama ?我不知道怎么做...我昨晚尝试搜索但仍然没有改变任何东西..对不起我的语法错误。谢谢你
修改1 无法从 mata_kuliah 表中获取数据 这是我用于打印数据的刀片语法
{{ $data['makul'] }}
答案 0 :(得分:0)
使用 with()功能:
$client_profile = Client::where([['cid', '=', $cid], ['is_delete', '=', 0]])->with(['status', 'group_no', 'caseworker', 'clerk', 'active_attendants'])->first()->toArray();
下面是我的模特:
class Client extends Model {
public function status() {
return $this->hasOne('App\Models\Status', 'sid', 'status');
}
public function group_no() {
return $this->hasOne('App\Models\ClientGroups', 'cgid', 'group_no');
}
public function caseworker() {
return $this->hasOne('App\Models\Caseworker', 'cwid', 'caseworker_name');
}
public function clerk() {
return $this->hasOne('App\Models\Supervisorsnurses', 'snid', 'clerk');
}
public function active_attendants() {
return $this->hasMany('App\Models\AssociatedTask', 'cid');
}
public function location() {
return $this->hasOne('App\Models\Locations', 'lid', 'location');
}
}