2017-01-09 4 views
2

私は壁、郵便、拘束という3つのモデルを持っています。彼らはすべてIDを持っています。Laravelの多対多リレーションシップにリンクされたモデルを取得するには?

Post 
id 

Wall 
id 

Constraint 
id 

Post_wall 
post_id 
wall_id 
constraint_id 

私はManyToManyリレーションを持っています。その関係のピボットテーブルには、私は列constraint_idを持っています。ウォールにリンクされたすべての投稿を取得するときは、ピボットテーブルにリンクされている拘束も必要です。

今はwithPivot( 'constraint_id')メソッドを使用してconstraint_idを取得できます。

return $this->belongsToMany('App\Wall') 
     ->withPivot('constraint_id') 

しかし、私はidではなくオブジェクトを望みます。これはLaravelでも可能ですか?

return $this->belongsToMany('App\Wall') 
     ->withPivot('constraint_id') 
     ->join('constraints', 'post_wall.constraint_id', '=', 'constraint.id'); 

私も制約テーブルに参加しようとしましたが、オブジェクトを返しませんでした。

+1

これまでに何を試みましたか? – Tosfera

+0

このリンクをチェックすると、あなたはここであなたの答えを見つけることができると思います:http://www.easylaravelbook.com/blog/2016/04/06/introducing-laravel-many-to-many-relations/ – amrdruid

+0

リンクしたページ - > withPivot( 'description')メソッドが見つかりました。これは説明を返します。私の状況では、これはPostオブジェクトにconstraint_idを配置しますが、PostオブジェクトのConstraintオブジェクトはIDだけでなく必要です。 –

答えて

関連する問題