然后可以这样使用:
$list = collection($list)->toArray();
$list = addtion($list, ['ktmingdan.kt_banji_id']);
在js中可以使用ktmingdan.kt_banji_name,获得关联名单表中的班级名称
文件位于:application/common.php`
/**
* 附加关联字段数据 * @param array $items 数据列表 * @param mixed $fields 渲染的来源字段 * <a href="https://ask.fastadmin.net/u/20697" data-type="user" data-id="20697" data-toggle="popover" data-title="Return">@return</a> array */function addtion($items, $fields){ if (!$items || !$fields) { return $items; } $fieldsArr = []; if (!is_array($fields)) { $arr = explode(',', $fields); foreach ($arr as $k => $v) { $fieldsArr[$v] = ['field' => $v]; } } else { foreach ($fields as $k => $v) { if (is_array($v)) { $v['field'] = isset($v['field']) ? $v['field'] : $k; } else { $v = ['field' => $v]; } $fieldsArr[$v['field']] = $v; } } foreach ($fieldsArr as $k => &$v) { $v = is_array($v) ? $v : ['field' => $v]; $v['display'] = isset($v['display']) ? $v['display'] : str_replace(['_ids', '_id'], ['_names', '_name'], $v['field']); $v['primary'] = isset($v['primary']) ? $v['primary'] : ''; $v['column'] = isset($v['column']) ? $v['column'] : 'name'; $v['model'] = isset($v['model']) ? $v['model'] : ''; $v['table'] = isset($v['table']) ? $v['table'] : ''; $v['name'] = isset($v['name']) ? $v['name'] : str_replace(['_ids', '_id'], '', $v['field']); } unset($v); $ids = []; $fields = array_keys($fieldsArr); foreach ($items as $k => $v) { foreach ($fields as $m => $n) { if (isset($v[$n])) { $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$n])); }elseif (strpos($n, '.')) { list($model_rel, $model_field) = explode('.', $n); $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$model_rel][$model_field])); } } } $result = []; foreach ($fieldsArr as $k => $v) { if ($v['model']) { $model = new $v['model']; } else { if (strpos($v['name'], '.')) { list($model_rel, $model_table) = explode('.', $v['name']); $model = $model_table ? thinkDb::name($model_table) : thinkDb::table($v['table']); }else{ $model = $v['name'] ? thinkDb::name($v['name']) : thinkDb::table($v['table']); } } $primary = $v['primary'] ? $v['primary'] : $model->getPk(); $result[$v['field']] = $model->where($primary, 'in', $ids[$v['field']])->column("{$primary},{$v['column']}"); } foreach ($items as $k => &$v) { foreach ($fields as $m => $n) { if (isset($v[$n])) { $curr = array_flip(explode(',', $v[$n])); $v[$fieldsArr[$n]['display']] = implode(',', array_intersect_key($result[$n], $curr)); }elseif (strpos($n, '.')) { list($model_rel, $model_field) = explode('.', $n); $curr = array_flip(explode(',', $v[$model_rel][$model_field])); list($model_rel, $model_display) = explode('.', $fieldsArr[$n]['display']); $v[$model_rel][$model_display] = implode(',', array_intersect_key($result[$n], $curr)); } } } return $items;}`
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。