Blogger Information
Blog 77
fans 0
comment 0
visits 55016
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实战2ele与tp:添加修改删除管理员,管理组操作(增删改查)
Jet的博客
Original
448 people have browsed it

一、涉及代码文件

文件:

  • shop/src/views/User.vue(更新:管理员新增/修改/删除操作)
  • shop/src/views/Group.vue(新增:管理组新增/修改/删除/搜索方法)
  • tp6/app/admin/controller/Index.php(更新:管理员新增/修改/删除操作方法、管理组新增/修改/删除/搜索方法,给User.vue和Group.vue访问提供数据)
  • shop/src/network/index.js(更新:管理员新增/修改/删除请求方法,管理组新增/修改/删除/请求方法)

1、User.vue文件代码

  1. <template>
  2. <div>
  3. <el-space wrap style="margin-bottom:20px;">
  4. <el-input v-model="data.search" size="small" placeholder="Type to search" />
  5. <el-button type="" @click="search()">搜索</el-button>
  6. </el-space>
  7. <el-space wrap>
  8. <el-button type="" @click="add()">添加</el-button>
  9. </el-space>
  10. <el-table
  11. :data="data.table"
  12. border
  13. style="width: 100%"
  14. @select="select"
  15. >
  16. <!--
  17. <el-table-column align="right">
  18. </el-table-column>
  19. <el-table-column label="选择" type="selection" />-->
  20. <el-table-column prop="uid" label="ID" min-width="50" />
  21. <el-table-column prop="name" label="Name" />
  22. <el-table-column prop="account" label="账号" />
  23. <el-table-column prop="phone" label="手机号" />
  24. <el-table-column prop="qq" label="QQ">
  25. </el-table-column>
  26. <el-table-column prop="sex" label="性别">
  27. <!--插槽-->
  28. <!-- scope 相当于循环里的一次数据
  29. foreach ( $data['table] as $v)
  30. scope.row 就是相当于是 $v
  31. -->
  32. <template #default="scope">
  33. <span v-if="scope.row.sex == 'mimi'">
  34. 保密
  35. </span>
  36. <span v-else-if="scope.row.sex == 'nan'">
  37. </span>
  38. <span v-else-if="scope.row.sex == 'nv'">
  39. </span>
  40. </template>
  41. </el-table-column>
  42. <el-table-column prop="uid" label="修改" width="160px;">
  43. <template #default="scope">
  44. <el-space wrap>
  45. <!--传点击这条数据的( scope.row )-->
  46. <el-button type="warning" @click="add(scope.row)">修改</el-button>
  47. </el-space>
  48. <el-space wrap>
  49. <el-button type="danger" @click="del(scope.row)">删除</el-button>
  50. </el-space>
  51. </template>
  52. </el-table-column>
  53. </el-table>
  54. <el-pagination
  55. layout="prev, pager, next"
  56. :total="data.total"
  57. :page-size="5"
  58. @current-change="fun"
  59. />
  60. <!--
  61. total:总条数
  62. page-size:一页显示多少条
  63. -->
  64. <!--添加/修改 弹窗-->
  65. <el-dialog v-model="data.add_form" :title="data.title">
  66. <el-form :model="form" label-width="120px">
  67. <el-form-item label="账号">
  68. <el-input v-model="form.account" />
  69. </el-form-item>
  70. <el-form-item label="密码">
  71. <el-input v-model="form.password" />
  72. </el-form-item>
  73. <el-form-item label="姓名">
  74. <el-input v-model="form.name" />
  75. </el-form-item>
  76. <el-form-item label="手机号">
  77. <el-input v-model="form.phone" />
  78. </el-form-item>
  79. <el-form-item label="QQ号">
  80. <el-input v-model="form.qq" />
  81. </el-form-item>
  82. <el-form-item label="性别">
  83. <el-select v-model="form.sex" class="m-2" placeholder="Select" size="large">
  84. <el-option label="男" value="nan" />
  85. <el-option label="女" value="nv" />
  86. <el-option label="保密" value="mimi" />
  87. </el-select>
  88. </el-form-item>
  89. </el-form>
  90. <template #footer>
  91. <span class="dialog-footer">
  92. <el-button @click="data.add_form = false">取消</el-button>
  93. <el-button type="primary" @click="add_form_json()">
  94. {{ data.title }}
  95. </el-button>
  96. </span>
  97. </template>
  98. </el-dialog>
  99. </div>
  100. </template>
  101. <script>
  102. import { reactive } from "vue"; // 引入动态创建
  103. import { UserLists , UserAdd ,UserDel } from "../network/index";
  104. export default{
  105. setup () {
  106. // 展示数据
  107. const data = reactive({
  108. table : [],
  109. total : 0,
  110. search : '',
  111. add_form : false,
  112. title : '添加',
  113. });
  114. // 展示的请求接口
  115. UserLists().then( (e)=>{
  116. data.table = e.data.data.user;
  117. data.total = e.data.data.count;
  118. console.log(data.table);
  119. })
  120. // 选择事件
  121. const select = (selection,row)=>{
  122. console.log(selection);
  123. console.log(row);
  124. }
  125. // 翻页方法
  126. const fun = (p) => {
  127. UserLists({p:p}).then( (e)=>{
  128. data.table = e.data.data.user;
  129. data.total = e.data.data.count;
  130. })
  131. // Index.php接收传值
  132. }
  133. // 搜索方法
  134. const search = () => {
  135. UserLists({search:data.search}).then( (e)=>{
  136. data.table = e.data.data.user;
  137. data.total = e.data.data.count;
  138. })
  139. }
  140. // 添加 修改
  141. // 展开form表单
  142. const form = reactive({
  143. uid : 0,
  144. account: '',
  145. password: '',
  146. name : '',
  147. phone : '',
  148. qq: '',
  149. sex: 'nan',
  150. })
  151. // 添加方法,如果为真,打开表单
  152. // 点击接收传uid数据,若有
  153. const add = (e) => {
  154. console.log(e);
  155. data.add_form = true;
  156. if(e){
  157. form.uid = e.uid;
  158. form.account = e.account;
  159. form.password = '';
  160. form.name = e.name;
  161. form.phone = e.phone;
  162. form.qq = e.qq;
  163. form.sex = e.sex;
  164. data.title = '修改';
  165. }else{
  166. form.uid = 0;
  167. form.account = '';
  168. form.password = '';
  169. form.name = '';
  170. form.phone = '';
  171. form.qq = '';
  172. form.sex = 'nan';
  173. data.title = '添加';
  174. }
  175. }
  176. // 确定提交添加的数据
  177. const add_form_json = () => {
  178. // 需要把数据提交给php接口
  179. // console.log(form);
  180. UserAdd(form).then((e) =>{
  181. // console.log(e);
  182. alert(e.data.msg);
  183. if(e.data.code == 0){
  184. UserLists().then( (e)=>{
  185. // 添加成功后自动刷新数据
  186. data.table = e.data.data.user;
  187. data.total = e.data.data.count;
  188. console.log(data.table);
  189. // 关闭添加表单
  190. data.add_form = false;
  191. })
  192. }
  193. })
  194. }
  195. // 删除
  196. const del = (e) => {
  197. UserDel({uid:e.uid}).then( (res) => {
  198. alert(res.data.msg);
  199. if( res.data.code == 0 ){
  200. UserLists().then( (e)=>{
  201. // 添加成功后自动刷新数据
  202. data.table = e.data.data.user;
  203. data.total = e.data.data.count;
  204. data.add_form = false;
  205. })
  206. }
  207. })
  208. }
  209. // 数据暴露出去
  210. return {
  211. data,
  212. select,
  213. fun,
  214. search,
  215. add,
  216. form,
  217. add_form_json,
  218. del,
  219. }
  220. }
  221. }
  222. </script>

2、Index.php文件代码

  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. class Index extends BaseController
  6. {
  7. public function __construct(){
  8. // 跨域请求
  9. header("Access-Control-Allow-Origin:*");
  10. }
  11. public function index()
  12. {
  13. return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
  14. }
  15. // 管理员列表
  16. public function UserLists(){
  17. $p = input('post.p',1);
  18. $search = input('post.search','');
  19. // 在查询数出来的时候,
  20. // $user = Db::table('xpcms_admin')->select()->toArray();
  21. // limit方法、pgae方法
  22. // count方法统计数据库有多少条数据
  23. $where = [];
  24. if(!empty($search)){
  25. $where1 = [['username','like','%'.$search.'%']];
  26. $where2 = [['real_name','like','%'.$search.'%']];
  27. $where3 = [['mobile','like','%'.$search.'%']];
  28. $where = [$where1,$where2,$where3];
  29. }
  30. $count = Db::table('bew_admin_user')->whereOr($where)->where('status','<>','2')->count();
  31. $user = Db::table('bew_admin_user')->whereOr($where)->where('status','<>','2')->page($p,5)->select()->toArray();
  32. // toArray 数组形式,显示时间戳
  33. foreach($user as &$u){
  34. $u['time_add_s'] = date('Y-m-d H:i:s',$u['time_add']);
  35. }
  36. $arr = [
  37. 'count' => $count,
  38. 'user' => $user,
  39. ];
  40. // php接口、只能有一次输出结果值
  41. echo json_encode([
  42. 'code' => 0,
  43. 'msg' => '成功',
  44. 'data' => $arr
  45. ]);
  46. }
  47. // 添加&修改管理员,接收数据
  48. public function UserAdd(){
  49. $post = input('post.');
  50. $data = [
  51. 'account' => $post['account'],
  52. 'name' => $post['name'],
  53. 'phone' => $post['phone'],
  54. 'qq' => $post['qq'],
  55. 'sex' => $post['sex'],
  56. 'time_last' => time(),
  57. ];
  58. // 查询单条数据
  59. $user = Db::table('bew_admin_user')->where('account',$post['account'])->find();
  60. if ( $post['uid'] == 0 ){
  61. // 判断查询出来的用户是否存在,若存在就退出,否则新建
  62. if(!empty($user)){
  63. echo json_encode([
  64. 'code' => 1,
  65. 'msg' => '账号已存在'
  66. ]);
  67. exit;
  68. }
  69. // 如果为0.就添加
  70. $data['password'] = md5($post['password']);
  71. $data['time_add'] = time();
  72. $ret = Db::table('bew_admin_user')->insert($data);
  73. } else {
  74. // 判断查询出来的用户名是否存在,且是不等于查询出来的uid,则修改
  75. if(!empty($user) && $post['uid'] != $user['uid']){
  76. echo json_encode([
  77. 'code' => 1,
  78. 'msg' => '账号已存在'
  79. ]);
  80. exit;
  81. }
  82. // 如果不为0 就修改
  83. if(!empty($post['password'])){
  84. $data['password'] = md5($post['password']);
  85. }
  86. $ret = Db::table('bew_admin_user')->where('uid', $post['uid'])->update($data);
  87. }
  88. // 添加&修改 后输出结果
  89. if( $post['uid'] == 0 ){
  90. if(empty($ret)){
  91. echo json_encode([
  92. 'code' => 1,
  93. 'msg' => '添加失败,请重试'
  94. ]);
  95. exit;
  96. };
  97. echo json_encode([
  98. 'code' => 0,
  99. 'msg' => '添加成功'
  100. ]);
  101. } else {
  102. if(empty($ret)){
  103. echo json_encode([
  104. 'code' => 1,
  105. 'msg' => '修改失败,请重试'
  106. ]);
  107. exit;
  108. };
  109. echo json_encode([
  110. 'code' => 0,
  111. 'msg' => '修改成功'
  112. ]);
  113. }
  114. }
  115. // 删除方法
  116. public function UserDel(){
  117. $uid = input('post.uid',0);
  118. if($uid ==0){
  119. echo json_encode([
  120. 'code' => 1,
  121. 'msg' => '请选择要删除的账号'
  122. ]);
  123. exit;
  124. }
  125. $del = Db::table('bew_admin_user')->where('uid', $uid)->update([
  126. 'status'=>2,
  127. 'time_last' => time(),
  128. ]);
  129. if(empty($del)){
  130. echo json_encode([
  131. 'code' => 1,
  132. 'msg' => '删除失败,请重试'
  133. ]);
  134. exit;
  135. }
  136. echo json_encode([
  137. 'code' => 0,
  138. 'msg' => '删除成功'
  139. ]);
  140. exit;
  141. }
  142. // 管理组列表接口
  143. public function GroupLists(){
  144. $p = input('post.p',1);
  145. $search = input('post.search','');
  146. $where = [];
  147. if(!empty($search)){
  148. $where[] = ['group_name','like','%'.$search.'%'];
  149. }
  150. $count = Db::table('bew_admin_user_group')->where('status','<>',0)->where($where)->count();
  151. $group = Db::table('bew_admin_user_group')->where('status','<>',0)->where($where)->page($p,3)->select()->toArray();
  152. foreach($group as &$u){
  153. $u['time_add_s'] = date('Y-m-d H:i:s',$u['time_add']);
  154. }
  155. $arr = [
  156. 'count' => $count,
  157. 'group' => $group,
  158. ];
  159. // php接口、只能有一次输出结果值
  160. echo json_encode([
  161. 'code' => 0,
  162. 'msg' => '成功',
  163. 'data' => $arr
  164. ]);
  165. }
  166. // 添加管理组
  167. public function GroupAdd(){
  168. $post = input('post.');
  169. $group = Db::table('bew_admin_user_group')->where('group_name', $post['group_name'])->find();
  170. if($post['group_id'] == 0){
  171. if(!empty($group)){
  172. echo json_encode([
  173. 'code' => 1,
  174. 'msg' => '添加失败,部门已存在'
  175. ]);
  176. exit;
  177. }
  178. $ret = Db::table('bew_admin_user_group')->insert([
  179. 'group_name' => $post['group_name'],
  180. 'status' => $post['status'],
  181. 'time_add' => time()
  182. ]);
  183. } else {
  184. if(!empty($group) && $group['group_name'] == $post['group_name']){
  185. echo json_encode([
  186. 'code' => 1,
  187. 'msg' => '修改失败,部门已存在'
  188. ]);
  189. exit;
  190. }
  191. $ret = Db::table('bew_admin_user_group')->where('group_id',$post['group_id'])->update([
  192. 'group_name' => $post['group_name'],
  193. 'status' => $post['status'],
  194. 'time_add' => time(),
  195. 'time_last' => time()
  196. ]);
  197. }
  198. if($post['group_id'] == 0 ){
  199. if(empty($ret)){
  200. echo json_encode([
  201. 'code' => 1,
  202. 'msg' => '添加失败,请重试'
  203. ]);
  204. exit;
  205. }
  206. echo json_encode([
  207. 'code' => 0,
  208. 'msg' => '添加成功'
  209. ]);
  210. } else {
  211. if(empty($ret)){
  212. echo json_encode([
  213. 'code' => 1,
  214. 'msg' => '修改失败,请重试'
  215. ]);
  216. exit;
  217. }
  218. echo json_encode([
  219. 'code' => 0,
  220. 'msg' => '修改成功'
  221. ]);
  222. }
  223. }
  224. // 删除管理组
  225. public function GroupDel(){
  226. $group_id = input('post.group_id',0);
  227. if($group_id == 0){
  228. echo json_encode([
  229. 'code' => 1,
  230. 'msg' => '请选择要删除的管理组'
  231. ]);
  232. exit;
  233. }
  234. $ret = Db::table('bew_admin_user_group')->where('group_id',$group_id)->update([
  235. 'status' => 0,
  236. 'time_last' => time()
  237. ]);
  238. if(empty($ret)){
  239. echo json_encode([
  240. 'code' => 1,
  241. 'msg' => '删除失败,请重试'
  242. ]);
  243. exit;
  244. }
  245. echo json_encode([
  246. 'code' => 0,
  247. 'msg' => '删除成功'
  248. ]);
  249. }
  250. public function hello($name = 'ThinkPHP6')
  251. {
  252. return 'hello,' . $name;
  253. }
  254. }

3、Group.vue文件代码

  1. <template>
  2. <div>
  3. <el-space wrap style="margin-bottom:20px;">
  4. <el-input v-model="data.search" size="small" placeholder="Type to search" />
  5. <el-button type="" @click="search()">搜索</el-button>
  6. </el-space>
  7. <el-space wrap>
  8. <el-button type="" @click="add()">添加</el-button>
  9. </el-space>
  10. <el-table
  11. :data="data.table"
  12. border
  13. style="width: 100%"
  14. @select="select"
  15. >
  16. <!--
  17. <el-table-column align="right">
  18. </el-table-column>
  19. <el-table-column label="选择" type="selection" />-->
  20. <el-table-column prop="group_id" label="ID" min-width="50" />
  21. <el-table-column prop="group_name" label="Name" />
  22. <el-table-column prop="status" label="状态">
  23. <!--插槽-->
  24. <!-- scope 相当于循环里的一次数据
  25. foreach ( $data['table] as $v)
  26. scope.row 就是相当于是 $v
  27. -->
  28. <template #default="scope">
  29. <span v-if="scope.row.status == 0">
  30. 关闭
  31. </span>
  32. <span v-else-if="scope.row.status == 1">
  33. 开启
  34. </span>
  35. </template>
  36. </el-table-column>
  37. <el-table-column prop="uid" label="修改" width="160px;">
  38. <template #default="scope">
  39. <el-space wrap>
  40. <!--传点击这条数据的( scope.row )-->
  41. <el-button type="warning" @click="add(scope.row)">修改</el-button>
  42. </el-space>
  43. <el-space wrap>
  44. <el-button type="danger" @click="del(scope.row)">删除</el-button>
  45. </el-space>
  46. </template>
  47. </el-table-column>
  48. </el-table>
  49. <el-pagination
  50. layout="prev, pager, next"
  51. :total="data.total"
  52. :page-size="3"
  53. @current-change="fun"
  54. />
  55. <!--
  56. total:总条数
  57. page-size:一页显示多少条
  58. -->
  59. <!--添加/修改 弹窗-->
  60. <el-dialog v-model="data.add_form" :title="data.title">
  61. <el-form :model="form" label-width="120px">
  62. <el-form-item label="部门名">
  63. <el-input v-model="form.group_name" />
  64. </el-form-item>
  65. <el-form-item label="状态">
  66. <el-select v-model="form.stutas" class="m-2" placeholder="Select" size="large">
  67. <el-option label="开启" value="1" />
  68. <el-option label="关闭" value="0" />
  69. </el-select>
  70. </el-form-item>
  71. </el-form>
  72. <template #footer>
  73. <span class="dialog-footer">
  74. <el-button @click="data.add_form = false">取消</el-button>
  75. <el-button type="primary" @click="add_form_json()">
  76. {{ data.title }}
  77. </el-button>
  78. </span>
  79. </template>
  80. </el-dialog>
  81. </div>
  82. </template>
  83. <script>
  84. import { reactive } from "vue"; // 引入动态创建
  85. import { GroupLists , GroupAdd ,GroupDel } from "../network/index";
  86. export default{
  87. setup () {
  88. // 展示数据
  89. const data = reactive({
  90. table : [],
  91. total : 0,
  92. search : '',
  93. add_form : false,
  94. title : '添加',
  95. });
  96. // 展示的请求接口
  97. GroupLists().then( (e)=>{
  98. data.table = e.data.data.group;
  99. data.total = e.data.data.count;
  100. console.log(data.table);
  101. })
  102. // 选择事件
  103. const select = (selection,row)=>{
  104. console.log(selection);
  105. console.log(row);
  106. }
  107. // 翻页方法
  108. const fun = (p) => {
  109. GroupLists({p:p}).then( (e)=>{
  110. data.table = e.data.data.group;
  111. data.total = e.data.data.count;
  112. })
  113. // Index.php接收传值
  114. }
  115. // 搜索方法
  116. const search = () => {
  117. GroupLists({search:data.search}).then( (e)=>{
  118. data.table = e.data.data.group;
  119. data.total = e.data.data.count;
  120. })
  121. }
  122. // 添加 修改
  123. // 展开form表单
  124. const form = reactive({
  125. group_id : 0,
  126. group_name: '',
  127. status: '',
  128. })
  129. // 添加方法,如果为真,打开表单
  130. // 点击接收传uid数据,若有
  131. const add = (e) => {
  132. console.log(e);
  133. data.add_form = true;
  134. if(e){
  135. form.group_id = e.group_id;
  136. form.group_name = e.group_name;
  137. form.status = e.status;
  138. data.title = '修改';
  139. }else{
  140. form.group_id = 0;
  141. form.group_name = '';
  142. form.status = '1';
  143. data.title = '添加';
  144. }
  145. }
  146. // 确定提交添加的数据
  147. const add_form_json = () => {
  148. // 需要把数据提交给php接口
  149. // console.log(form);
  150. GroupAdd(form).then((e) =>{
  151. // console.log(e);
  152. alert(e.data.msg);
  153. if(e.data.code == 0){
  154. GroupLists().then( (e)=>{
  155. // 添加成功后自动刷新数据
  156. data.table = e.data.data.group;
  157. data.total = e.data.data.count;
  158. console.log(data.table);
  159. // 关闭添加表单
  160. data.add_form = false;
  161. })
  162. }
  163. })
  164. }
  165. // 删除
  166. const del = (e) => {
  167. GroupDel({group_id:e.group_id}).then( (res) => {
  168. alert(res.data.msg);
  169. if( res.data.code == 0 ){
  170. GroupLists().then( (e)=>{
  171. // 添加成功后自动刷新数据
  172. data.table = e.data.data.group;
  173. data.total = e.data.data.count;
  174. data.add_form = false;
  175. })
  176. }
  177. })
  178. }
  179. // 数据暴露出去
  180. return {
  181. data,
  182. select,
  183. fun,
  184. search,
  185. add,
  186. form,
  187. add_form_json,
  188. del,
  189. }
  190. }
  191. }
  192. </script>

4、index.js文件代码

  1. import { request } from "./request.js";
  2. // 跳转至tp项目文件
  3. export function login(data){
  4. return request ({
  5. url : "Login/index",
  6. method: "POST",
  7. data
  8. })
  9. }
  10. export function UserLists(data){
  11. return request ({
  12. url : "Index/UserLists",
  13. method: "POST",
  14. data
  15. })
  16. }
  17. // 添加数据请求
  18. export function UserAdd(data){
  19. return request ({
  20. url : "Index/UserAdd",
  21. method: "POST",
  22. data
  23. })
  24. }
  25. // 修改数据请求
  26. export function UserEdit(data){
  27. return request ({
  28. url : "Index/UserEdit",
  29. method: "POST",
  30. data
  31. })
  32. }
  33. // 删除数据请求
  34. export function UserDel(data){
  35. return request ({
  36. url : "Index/UserDel",
  37. method: "POST",
  38. data
  39. })
  40. }
  41. // 展示管理组请求
  42. export function GroupLists(data){
  43. return request ({
  44. url : "Index/GroupLists",
  45. method: "POST",
  46. data
  47. })
  48. }
  49. // 新增管理组请求
  50. export function GroupAdd(data){
  51. return request ({
  52. url : "Index/GroupAdd",
  53. method: "post",
  54. data
  55. })
  56. }
  57. // 删除管理组请求
  58. export function GroupDel(data){
  59. return request ({
  60. url : "Index/GroupDel",
  61. method: "post",
  62. data
  63. })
  64. }

5、运行截图:







6、踩坑点:

6.1、SQL查询语句:

  • 方法1:
  1. $where = [];
  2. if(!empty($search)){
  3. $where1 = [['account','like','%'.$search.'%']];
  4. $where2 = [['name','like','%'.$search.'%']];
  5. $where3 = [['phone','like','%'.$search.'%']];
  6. $where = [$where1,$where2,$where3];
  7. }
  8. $count = Db::table('bew_admin_user')->whereOr($where)->where('status','<>','2')->count();
  9. $user = Db::table('bew_admin_user')->whereOr($where)->where('status','<>','2')->page($p,5)->select()->toArray();


  • 方法2:
  1. $where = [];
  2. if(!empty($search)){
  3. $where1 = [['account','like','%'.$search.'%']];
  4. $where2 = [['name','like','%'.$search.'%']];
  5. $where3 = [['phone','like','%'.$search.'%']];
  6. $where = [$where1,$where2,$where3];
  7. }
  8. $count = Db::table('bew_admin_user')->where('status','<>','2')->whereOr($where)->count();
  9. $user = Db::table('bew_admin_user')->where('status','<>','2')->whereOr($where)->page($p,5)->select()->toArray();


  1. 由此可见:whereOr在前,where在后,会先判断whereOr后继续判断where
  2. 如果where在前,whereOr在后,判断完where就不再判断whereOr

6.2、跨域问题

出现错误,有可能是因为跨域问题,或者是代码写错

  1. 1、跨域解决:添加一个魔术方法
  2. public function __construct(){
  3. // 跨域请求
  4. header("Access-Control-Allow-Origin:*");
  5. }
  6. 2、代码写错,详情检查了
  7. 如:
  8. 正确:$ret = Db::table('bew_admin_user')->insert($data);
  9. 错误:insert([$data])
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post