关联表查询
数据库posts:
id | title | type |
---|---|---|
1 | 标题1 | 1 |
2 | 标题2 | 3 |
3 | 标题3 | 2 |
数据库class:
id | title |
---|---|
1 | 类型1 |
2 | 类型2 |
3 | 类型3 |
posts表的type对应class表的id;
我想查询结果是
[
'id'=>1,
'title'=>'标题1',
'type'=>1,
'type_title'=>'类型1' // 就是这个地方可以用posts表的type字段关联到class表的title字段
]
所以查询语句应该咋写?模型查询应该咋写?
我当前的办法是
$posts = posts::query()->where('id',1)->first();
$posts['type_title'] = class::query()->where('id',$posts['type_name'])->->first()['title'];
我觉得这样写好蠢;但是也不会高级的办法。请指教;
推荐文章: