闭包,查出来的赋值给topic_num属性 $chapterObj = (new TopicChapter()) ->where('question_bank_id', '=', $questionBankId) ->where('is_deleted', '=', 0) ->where('chapter_type', '=', 1) ->order('sort ASC') ->field('id as chapter_id,chapter_name') ->select(); $chapterObj->each(function ($item) use ($questionBankId) { $item->topic_num = (new \app\common\model\v2\Topic())->where(['question_bank_id' => $questionBankId, 'topic_chapter_id' => $item->chapter_id, 'parent_id' => 0, 'is_deleted' => 0, 'organization_id' => 3])->count('id'); });