get('q'); if ($q != null) { // 模糊搜索 $obj = new DistProduct(); return $obj->model()->where('title', 'like', "%$q%")->paginate(null, ['id', 'title as text']); } else { // 获取最新的50个 $selectOptionsNew = DistProduct::selectOptionsNew(); return $this->changeOptions($selectOptionsNew); } } public function pages(Request $request) { $q = $request->get('q'); if ($q != null) { // 模糊搜索 $obj = new SitePages(); return $obj->model()->where('title', 'like', "%$q%")->paginate(null, ['id', 'title as text']); } else { // 获取最新的50个 $selectOptionsNew = SitePages::selectOptionsNew(); return $this->changeOptions($selectOptionsNew); } } public function tag(Request $request) { $q = $request->get('q'); if ($q != null) { // 模糊搜索 $obj = new SitePagesTag(); return $obj->model()->where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']); } else { // 获取最新的50个 $selectOptionsNew = SitePagesTag::selectOptionsNew(); return $this->changeOptions($selectOptionsNew); } } private function changeOptions($data) { // 初始化结果数组 $result = []; // 遍历原始数据并转换格式 foreach ($data as $id => $text) { $result[] = [ 'id' => (int)$id, // 将字符串转换为整数 'text' => $text ]; } return $result; } }