Php Laravel Connect帖子和类别
我有三张桌子Php Laravel Connect帖子和类别,php,laravel-4,Php,Laravel 4,我有三张桌子 Posts with ID Post_Categories with ID, Post_ID and Category_id Categories with ID 我需要为一个特定的帖子获取所有类别,有人能帮我吗?这是经典的多对多关系,在Laravel中,是这样做的: class Post { function categories() { return $this->belongsToMany('Category', 'Post_Cat
Posts with ID
Post_Categories with ID, Post_ID and Category_id
Categories with ID
我需要为一个特定的帖子获取所有类别,有人能帮我吗?这是经典的多对多关系,在
Laravel
中,是这样做的:
class Post
{
function categories()
{
return $this->belongsToMany('Category', 'Post_Categories');
}
}
将此代码添加到Posts模型
public function categories()
{
return $this->belongsToMany('Category', 'categories_posts','Category_ID','Post_ID') ;
}
然后把一篇文章的所有类别都抽出来,你只需要这样做
$myPost = Post::find(idPost);
$myPost->categories ;