php - Many to many relationship and where in Laravel 4 -
in laravel 4, when working many-to-many relationships, following:
/* skills 1 specific topic */ route::get( '/api/topics/{topicslug}/skills', function( $topicslug ) { $skills = skill::where( 'topic.slug', '=', $topicslug )->get(); }
now problem topic.slug
isn't column in skills
table. instead, slug
column in topics
table, related skills
table via pivot table called skills_topics
(which includes skill_id
, topic_id
).
i've tried number of variations, can't query right. how query have like, if want skills 1 particular topic?
the models are:
class skill extends eloquent { public function topics() { return $this->belongstomany( 'topic' ); } }
and
class topic extends eloquent { public function skills() { return $this->belongstomany( 'skill' ); } }
well should topic related skills if understood correctly. this:
$topic = topic::with('skills')->where('slug', $topicslug)->first();
and access skills through:
foreach ($topic->skills $skill) { }
Comments
Post a Comment