首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >拉力维尔轴心关系

拉力维尔轴心关系
EN

Stack Overflow用户
提问于 2018-10-14 09:47:47
回答 1查看 138关注 0票数 0

我使用的是Laravel5.7,我有以下表格

范畴

代码语言:javascript
运行
复制
id | category_name 

post_categories

代码语言:javascript
运行
复制
id | category_id | post_id | some other fields

发布

代码语言:javascript
运行
复制
id | post_title

我属于范畴模型中的许多关系

代码语言:javascript
运行
复制
 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,我可以避免类别细节在我的响应

我的目标是按类别检索所有的帖子。

EN

回答 1

Stack Overflow用户

发布于 2018-10-14 10:03:27

您可以在“类别”上使用select()函数删除不必要的列。

请注意,类别表的'id‘很重要,因为它作为外键在透视表中使用。

代码语言:javascript
运行
复制
// this will only get the id of the category
// and all the post and pivot data.
$response = Category::select('id')->with('post')->get();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52801422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档