2016-07-16 134 views
2

我想显示与profile表链接的user表中的数据。我得到了我的vardump中的数据,但我不知道如何一对一地显示它。 这是我的函数:Laravel不显示数据

public function show($id) 
{ 
    //Get data from user 
    $user = User::find($id); 
    //Get the horse count connect to the user 
    $horse_count = DB::table('horses')->where('user_id', $id)->count(); 

    var_dump($user->profile); 
    die(); 
} 

它返回:

object(Illuminate\Database\Eloquent\Collection)#188 (1) { 
    ["items":protected]=> 
    array(1) { 
    [0]=> 
    object(App\Profile)#189 (24) { 
     ["connection":protected]=> 
     NULL 
     ["table":protected]=> 
     NULL 
     ["primaryKey":protected]=> 
     string(2) "id" 
     ["keyType":protected]=> 
     string(3) "int" 
     ["perPage":protected]=> 
     int(15) 
     ["incrementing"]=> 
     bool(true) 
     ["timestamps"]=> 
     bool(true) 
     ["attributes":protected]=> 
     array(10) { 
     ["id"]=> 
     int(1) 
     ["user_id"]=> 
     int(2) 
     ["country"]=> 
     string(11) "Netherlands" 
     ["age"]=> 
     string(2) "21" 
     ["recent_online"]=> 
     string(0) "" 
     ["visitors"]=> 
     string(4) "2143" 
     ["profile"]=> 
     string(17) "Hoi ik ben Martin" 
     ["remember_token"]=> 
     NULL 
     ["created_at"]=> 
     NULL 
     ["updated_at"]=> 
     NULL 
     } 
     ["original":protected]=> 
     array(10) { 
     ["id"]=> 
     int(1) 
     ["user_id"]=> 
     int(2) 
     ["country"]=> 
     string(11) "Netherlands" 
     ["age"]=> 
     string(2) "21" 
     ["recent_online"]=> 
     string(0) "" 
     ["visitors"]=> 
     string(4) "2143" 
     ["profile"]=> 
     string(17) "Hoi ik ben Martin" 
     ["remember_token"]=> 
     NULL 
     ["created_at"]=> 
     NULL 
     ["updated_at"]=> 
     NULL 
     } 
     ["relations":protected]=> 
     array(0) { 
     } 
     ["hidden":protected]=> 
     array(0) { 
     } 
     ["visible":protected]=> 
     array(0) { 
     } 
     ["appends":protected]=> 
     array(0) { 
     } 
     ["fillable":protected]=> 
     array(0) { 
     } 
     ["guarded":protected]=> 
     array(1) { 
     [0]=> 
     string(1) "*" 
     } 
     ["dates":protected]=> 
     array(0) { 
     } 
     ["dateFormat":protected]=> 
     NULL 
     ["casts":protected]=> 
     array(0) { 
     } 
     ["touches":protected]=> 
     array(0) { 
     } 
     ["observables":protected]=> 
     array(0) { 
     } 
     ["with":protected]=> 
     array(0) { 
     } 
     ["morphClass":protected]=> 
     NULL 
     ["exists"]=> 
     bool(true) 
     ["wasRecentlyCreated"]=> 
     bool(false) 
     } 
    } 
} 

但是,当我echo $user->profile->age;它说明不了什么。 有关信息,我将表userprofilereturn $this->belongsTo('App\User');return $this->hasMany('App\Profile');相互链接。我得到的数据,但我不能一对一显示。

+0

什么是在这种情况下个人资料? –

+0

你可能想在这种情况下使用急切加载。查看laravel文档以获取更多详细信息。 – ClearBoth

+0

配置文件是与用户表中的id链接的表 – twoam

回答

3

每一个一个:

foreach (User::find($id)->profile as $profile) { 
    // Do what you want, e.g. 
    echo $profile->age; 
} 

记住,如果用户有很多的配置文件,则关系函数名应该是复数像public function profiles()

+0

它似乎工作,但我不明白我是如何得到这个刀片?你可以帮我吗? – twoam

+0

当然!您可以将'$ user = User :: find($ id)'对象传递给视图(例如'return view('home.index',compact('user'));'。刀片功能:'@foreach($ user-> profile as $ profile){{$ profile-> age}} @ endforeach' –

+0

你摇滚!谢谢! – twoam