当前位置: 首页 > 工具软件 > Each > 使用案例 >

TP6 的 each 遍历用法

颛孙森
2023-12-01
闭包,查出来的赋值给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');
});
 类似资料: