/** * 关联分类表 */ public function categories() { return $this->belongsToMany('AudioCategoryModel', 'portal_category_post', 'category_id', 'post_id'); }
id=27,是内容表的27记录
$id = $this->request->param('id', 0, 'intval'); $AudioPostModel = new AudioPostModel(); $post = $AudioPostModel->where('id', $id)->find(); $postCategories = $post->categories()->alias('a')->column('a.name', 'a.id'); $postCategoryIds = implode(',', array_keys($postCategories)); dump($postCategoryIds); dump($postCategories);
dump出来的数据
string(5) "10,11"
array(2) {
[10] => string(12) "最新资讯"
[11] => string(12) "话术分享"
}
分类表
中间表 27是内容表的id
内容表
本来中间表post_id关联的是内容表的id,现在我想修改为把post_id关联内容表的post_id,内容表的post_id是时间戳。
整个执行逻辑应该是先拿内容表的id在中间表中筛选出的post_id等于id的数据,然后在把得到的category_id到分类表查找取得对应的名称。
然而我再上面的代码中没有找到第一步也就是拿内容表id查找中间表数据的语句,这样就没办法改成通过post_id查找数据。