tp6在模型關(guān)聯(lián)中獲取指定字段
作者:網(wǎng)站建設(shè) | 發(fā)布日期:2020-12-30
現(xiàn)在是要獲取一篇文章的具體信息,包括文章信息,文章所屬用戶,文章的所有評論,所有評論的用戶信息,所有評論的贊,所有贊的用戶信息,代碼如下
$issue = $this->issueModel->where('id', '=', $id)
->field('id,user_id,title,content,view_count,created_at,type_name')
->with(['user' => function($query) {
$query->field('member_id,username,headimgurl');
},'comment.approve.user'])
->find();
但是這樣的話,在comment.approve.user中,就無法設(shè)置獲取所有字段;
并且在with中我只是寫了comment.approve.user(只是為了獲取所有點(diǎn)贊的用戶信息),但是出來的結(jié)果卻是包含了所有的評論和所有的點(diǎn)贊相關(guān)的信息,這樣的話,該怎么設(shè)置獲取指定的字段信息呢?
with([
'comment' => function($query) {
$query->with([
'approve' => function($query) {
$query->with([
'user' => function($query) {
$query->field('xxx');
},
])->field('xx');
},
])->field('x');
},
])