经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
TP6框架--EasyAdmin学习笔记:实现数据库增删查改
来源:cnblogs  作者:林恒  时间:2021/5/24 10:57:41  对本文有异议

这是我写的学习EasyAdmin的第三章,这一章我给大家分享下如何进行数据库的增删查改

上一章链接:点击这里前往

上一章我们说到,我仿照官方案例,定义了一条路由goodsone和创建了对应数据库,我们可以看到view复制goodsone的文件夹中又这么几个文件

这些文件中,index.html是我们看到的列表页面,因为easyadmin前端采用的是layui,所有我们看到的内容是这样的

 我们可以看到很明显的layui痕迹,这里中增删改查已经又框架默认方法,路由的格式如上图设置即可

页面效果如下:

 这里没有layui知识的小伙伴会有一个疑问,页面中的数据和按钮是怎么出来的,上章定义路由的过程中,每一个路由都需要一个对应的js文件,这里的表单和按钮就是在哪里设置的,内容如下:

  1. define(["jquery", "easy-admin"], function ($, ea) {
  2. var init = {
  3. table_elem: '#currentTable',
  4. table_render_id: 'currentTableRenderId',
  5. index_url: 'mall.goodsone/index',
  6. add_url: 'mall.goodsone/add',
  7. edit_url: 'mall.goodsone/edit',
  8. delete_url: 'mall.goodsone/delete',
  9. export_url: 'mall.goodsone/export',
  10. modify_url: 'mall.goodsone/modify',
  11. stock_url: 'mall.goodsone/stock',
  12. };
  13. var Controller = {
  14. index: function () {
  15. ea.table.render({
  16. init: init,
  17. toolbar: ['refresh',
  18. [{
  19. text: '添加',
  20. url: init.add_url,
  21. method: 'open',
  22. auth: 'add',
  23. class: 'layui-btn layui-btn-normal layui-btn-sm',
  24. icon: 'fa fa-plus ',
  25. extend: 'data-full="true"',
  26. }],
  27. 'delete', 'export'],
  28. cols: [[
  29. {type: "checkbox"},
  30. {field: 'id', width: 80, title: 'ID'},
  31. {field: 'sort', width: 80, title: '排序', edit: 'text'},
  32. {field: 'cate.title', minWidth: 80, title: '商品分类'},
  33. {field: 'title', minWidth: 80, title: '商品名称'},
  34. {field: 'logo', minWidth: 80, title: '分类图片', search: false, templet: ea.table.image},
  35. {field: 'market_price', width: 100, title: '市场价', templet: ea.table.price},
  36. {field: 'discount_price', width: 100, title: '折扣价', templet: ea.table.price},
  37. {field: 'total_stock', width: 100, title: '库存统计'},
  38. {field: 'stock', width: 100, title: '剩余库存'},
  39. {field: 'virtual_sales', width: 100, title: '虚拟销量'},
  40. {field: 'sales', width: 80, title: '销量'},
  41. {field: 'status', title: '状态', width: 85, search: 'select',selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch},
  42. {field: 'create_time', minWidth: 80, title: '创建时间'},
  43. {
  44. width: 250,
  45. title: '操作',
  46. templet: ea.table.tool,
  47. operat: [
  48. [{
  49. text: '编辑',
  50. url: init.edit_url,
  51. method: 'open',
  52. auth: 'edit',
  53. class: 'layui-btn layui-btn-xs layui-btn-success',
  54. extend: 'data-full="true"',
  55. },
  56. // {
  57. // text: '入库',
  58. // url: init.stock_url,
  59. // method: 'open',
  60. // auth: 'stock',
  61. // class: 'layui-btn layui-btn-xs layui-btn-normal',
  62. // }
  63. ],
  64. 'delete']
  65. }
  66. ]],
  67. });
  68. ea.listen();
  69. },
  70. add: function () {
  71. ea.listen();
  72. },
  73. edit: function () {
  74. ea.listen();
  75. },
  76. stock: function () {
  77. ea.listen();
  78. },
  79. };
  80. return Controller;
  81. });

上方的代码大家可以清晰的看到各个增删查改的路由,直接照抄即可,layui大佬可以直接根据项目来修改,而对应的路由代码是放在controller层,代码如下大家而可以参考:

  1. <?php
  2. namespace app\admin\controller\mall;
  3. use app\admin\model\MallGoodsOne;
  4. use app\admin\traits\Curd;
  5. use app\common\controller\AdminController;
  6. use EasyAdmin\annotation\ControllerAnnotation;
  7. use EasyAdmin\annotation\NodeAnotation;
  8. use think\Facade\Db;
  9. use think\App;
  10. /**
  11. * Class Goods
  12. * @package app\admin\controller\mall
  13. * @ControllerAnnotation(title="商城商品管理")
  14. */
  15. class GoodsOne extends AdminController
  16. {
  17. use Curd;
  18. protected $relationSearch = true;
  19. public function __construct(App $app)
  20. {
  21. parent::__construct($app);
  22. $this->model = new MallGoodsOne();
  23. }
  24. /**
  25. * @NodeAnotation(title="列表")
  26. */
  27. public function index()
  28. {
  29. //var_dump($this->request->isAjax());exit();
  30. if ($this->request->isAjax()) {
  31. if (input('selectFields')) {
  32. return $this->selectList();
  33. }
  34. list($page, $limit, $where) = $this->buildTableParames();
  35. $count = $this->model
  36. ->withJoin('cate', 'LEFT')
  37. ->where($where)
  38. ->count();
  39. $list = $this->model
  40. ->withJoin('cate', 'LEFT')
  41. ->where($where)
  42. ->page($page, $limit)
  43. ->order($this->sort)
  44. ->select();
  45. $data = [
  46. 'code' => 0,
  47. 'msg' => '',
  48. 'count' => $count,
  49. 'data' => $list,
  50. ];
  51. return json($data);
  52. }
  53. return $this->fetch();
  54. }
  55. }

如果本文对你有所帮助,麻烦你点个赞,下一章讲下如何在EasyAdmin中用php来实现excel导入表中。

 

原文链接:http://www.cnblogs.com/smileZAZ/p/14784088.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号