我使用的是Laravel5.7,我有以下表格
范畴
id | category_name post_categories
id | category_id | post_id | some other fields发布
id | post_title我属于范畴模型中的许多关系
public function post(){
return $this->belongsToMany(Post::class,'post_categories','category_id','post_id')
->withPivot('col2', 'col3','col4');
}
$response=Category::with('post')->get();这将返回预期,但现在我不需要类别细节在我的响应,我的意思是,它有可能声明关系的枢轴模型,因为我知道category_id,我可以避免类别细节在我的响应
我的目标是按类别检索所有的帖子。
发布于 2018-10-14 10:03:27
您可以在“类别”上使用select()函数删除不必要的列。
请注意,类别表的'id‘很重要,因为它作为外键在透视表中使用。
// this will only get the id of the category
// and all the post and pivot data.
$response = Category::select('id')->with('post')->get();https://stackoverflow.com/questions/52801422
复制相似问题