Laravel 4 showing all field from one to one relationship in

Posted by rivai04 on Stack Overflow See other posts from Stack Overflow or by rivai04
Published on 2014-08-21T16:09:20Z Indexed on 2014/08/21 16:20 UTC
Read the original article Hit count: 381

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about php

Related posts about laravel