Laravel 4 showing all field from one to one relationship in
- by rivai04
I'm trying to show all field of the chosen row from one to one relationship...
Route
Route::get('relasi-pasien', function()
{
$pasien = PasienIri::where('no_ipd', '=', '100')->first();
foreach($pasien->keadaanumum as $temp)
{
echo'<li>
Name : '.$temp->name.
'Tekdar: '.$temp->tekdar.
'Nadi : '.$temp->nadi.
'</li>';
}
});
Relation at PasienIri's model
public function keadaanumum()
{
return $this->hasOne('KeadaanUmum', 'no_ipd');
}
Relation at KeadaanUmum's Model
public function pasieniri()
{
return $this->belongsTo('PasienIri', 'no_ipd');
}
When I used that way, it showed error: 'Trying to get property of non-object'
But if I just trying to show only one of the field, it works,
showing one field:
Route::get('relasi-pasien', function()
{
$pasien = PasienIri::where('no_ipd', '=', '100')->first();
return $pasien->keadaanumum->name;
});
anyone could help me to show all field with one to one relationship
or I really have to change it to one to many relationship? cause if I change it to one to many relationship, it works