TP6框架--EasyAdmin学习笔记:实现数据库增删查改

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

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

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

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

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

页面效果如下:

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

define(["jquery", "easy-admin"], function ($, ea) {
 
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'mall.goodsone/index',
        add_url: 'mall.goodsone/add',
        edit_url: 'mall.goodsone/edit',
        delete_url: 'mall.goodsone/delete',
        export_url: 'mall.goodsone/export',
        modify_url: 'mall.goodsone/modify',
        stock_url: 'mall.goodsone/stock',
    };
 
    var Controller = {
 
        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh',
                    [{
                        text: '添加',
                        url: init.add_url,
                        method: 'open',
                        auth: 'add',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                        extend: 'data-full="true"',
                    }],
                    'delete', 'export'],
                cols: [[
                    {type: "checkbox"},
                    {field: 'id', width: 80, title: 'ID'},
                    {field: 'sort', width: 80, title: '排序', edit: 'text'},
                    {field: 'cate.title', minWidth: 80, title: '商品分类'},
                    {field: 'title', minWidth: 80, title: '商品名称'},
                    {field: 'logo', minWidth: 80, title: '分类图片', search: false, templet: ea.table.image},
                    {field: 'market_price', width: 100, title: '市场价', templet: ea.table.price},
                    {field: 'discount_price', width: 100, title: '折扣价', templet: ea.table.price},
                    {field: 'total_stock', width: 100, title: '库存统计'},
                    {field: 'stock', width: 100, title: '剩余库存'},
                    {field: 'virtual_sales', width: 100, title: '虚拟销量'},
                    {field: 'sales', width: 80, title: '销量'},
                    {field: 'status', title: '状态', width: 85, search: 'select',selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch},
                    {field: 'create_time', minWidth: 80, title: '创建时间'},
                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [{
                                text: '编辑',
                                url: init.edit_url,
                                method: 'open',
                                auth: 'edit',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                                extend: 'data-full="true"',
                            },
                            // {
                            //     text: '入库',
                            //     url: init.stock_url,
                            //     method: 'open',
                            //     auth: 'stock',
                            //     class: 'layui-btn layui-btn-xs layui-btn-normal',
                            // }
                            ],
                            'delete']
                    }
                ]],
            });
 
            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        stock: function () {
            ea.listen();
        },
    };
    return Controller;
});

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

<?php
 
 
namespace app\admin\controller\mall;
 
 
use app\admin\model\MallGoodsOne;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\Facade\Db;
use think\App;
 
/**
 * Class Goods
 * @package app\admin\controller\mall
 * @ControllerAnnotation(title="商城商品管理")
 */
class GoodsOne extends AdminController
{
 
    use Curd;
 
    protected $relationSearch = true;
 
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallGoodsOne();
    }
 
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        //var_dump($this->request->isAjax());exit();
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->withJoin('cate', 'LEFT')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin('cate', 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
}

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

 

posted @ 2021-05-19 10:38  林恒  阅读(1708)  评论(0编辑  收藏  举报