2016-07-16 9 views
2

profileテーブルにリンクされたuserテーブルのデータを表示したいとします。私は私の弁当でデータを取得しますが、私はそれを1対1で表示する方法を知らない。それがこれを返す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;ときには何も表示されません。 私はテーブルuserprofileをお互いにreturn $this->belongsTo('App\User');return $this->hasMany('App\Profile');にリンクしました。私はデータを取得しますが、私はそれを1つ1つ表示することはできません。

+0

のようなユーザーが多くのプロファイルを持っているならば、関係の関数名が複数である必要があり、覚えていますか? –

+0

この場合、eager loadingを使用することができます。詳細は、laravelのドキュメントを参照してください。 – ClearBoth

+0

プロファイルは、ユーザーテーブル – twoam

答えて

3

1つあたりに:

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

関連する問題