TP6框架(jia)--EasyAdmin學習筆記:實(shi)現數據庫(ku)增刪(shan)查改
這是我寫的學習EasyAdmin的第三章,這一章我給大家分享下如何進行數據庫的增刪查改
上一章鏈接:點擊這里前往
上一(yi)章我們(men)說到(dao)(dao),我仿照官方案例,定義了一(yi)條(tiao)路由goodsone和(he)創建了對(dui)應數據庫,我們(men)可以看到(dao)(dao)view復制goodsone的文件夾中(zhong)又這么幾(ji)個文件

這(zhe)些文件中,index.html是我們(men)看到(dao)的(de)列表(biao)頁面,因(yin)為easyadmin前(qian)端采(cai)用的(de)是layui,所(suo)有我們(men)看到(dao)的(de)內容是這(zhe)樣的(de)

我們(men)可以看到很明顯的(de)layui痕跡(ji),這(zhe)里中增(zeng)刪改查(cha)已經又框(kuang)架默(mo)認方法,路由(you)的(de)格(ge)式(shi)如上圖設置即可
頁面效果如下:

這(zhe)里(li)沒(mei)有layui知識的(de)小(xiao)伙伴(ban)會有一個(ge)疑問,頁(ye)面中的(de)數據和按鈕(niu)是(shi)怎么出來的(de),上章定義路由的(de)過程中,每(mei)一個(ge)路由都需要一個(ge)對應的(de)js文件,這(zhe)里(li)的(de)表單和按鈕(niu)就是(shi)在哪里(li)設置的(de),內(nei)容如下:
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;
});
上方的(de)代碼大(da)(da)家可(ke)以(yi)清晰的(de)看到各(ge)個增刪(shan)查(cha)改的(de)路由(you),直接(jie)照抄即可(ke),layui大(da)(da)佬可(ke)以(yi)直接(jie)根據項(xiang)目來修(xiu)改,而對應(ying)的(de)路由(you)代碼是放在controller層,代碼如(ru)下(xia)大(da)(da)家而可(ke)以(yi)參考(kao):
<?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導入表中。
