侧边栏壁纸
博主头像
云BLOG 博主等级

行动起来,活在当下

  • 累计撰写 318 篇文章
  • 累计创建 6 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录
WEB

THINKPHP8+LAYUI的前后端条件查询示例

Administrator
2025-11-26 / 0 评论 / 0 点赞 / 5 阅读 / 0 字
public function getStockBatchInfo($stockid = null, $goodsname = null, $batchno = null, $page = 1, $limit = 10)
{
    // $params = request()->param();
    // $stockid = $params['stockid'] ?? null;
    // $goodsname = $params['goodsname'] ?? null;
    // $batchno = $params['batchno'] ?? null;
    // $page = $params['page'] ?? 1;
    // $limit = $params['limit'] ?? 10;

    try {
        $query = Db::name('glg_stock');
        
        // 只有当条件存在时才添加到查询中
        if (!empty($stockid)) {
            $query->where('stockid', $stockid);
        }
        
        if (!empty($goodsname)) {
            $query->where('goodsname', $goodsname); //  $query->where('goodsname', 'like', '%' . $goodsname . '%'); // 支持模糊查询
        }
        
        if (!empty($batchno)) {
            $query->where('batchno', $batchno);
        }
        
        // 获取总数
        $count = $query->count();
        
        // 获取分页数据
        $result = $query->page($page, $limit)->select(); //  $result = $query->select();
       
        
        return json([
            'code' => 0,  // layui成功的code为0
            'msg' => '查询成功',
            'count' => $count, // count($result),
            'data' => $result
        ]);
        
    } catch (\Exception $e) {
        return json([
            'code' => 1,  // layui错误的code通常为1或其他非0值
            'msg' => $e->getMessage(),
            'count' => 0,
            'data' => []
        ]);
    }
}


// layui table配置示例
table.render({
    elem: '#stockTable',
    url: '/your-controller/getStockInfo',
    method: 'post',
    where: {
        stockid: 'your_stockid_value',
        goodsname: 'your_goodsname_value',
        batchno: 'your_batchno_value'
    },
    cols: [[
        {field: 'stockid', title: '库存ID'},
        {field: 'goodsname', title: '商品名称'},
        {field: 'batchno', title: '批次号'},
        // 其他字段...
    ]],
    page: true
});

0

评论区