fastadmin中关联查询中控制器的index方法解析

public function index()
{
    if ($this->request->isAjax())//判断是否请求isAjax()方法,限制只有ajax请求时才去查询数据
    {
        //list构造参数(查询条件,主键,排序规则,选择页数后覆盖的数据,页面显示的条数)= 调用基类中的buildparams()方法,去生成查询所需条件
        //位置application/common/controller/Backend.php
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        //搜索数据条数$total,搜索具体数据$list
        //$this->model:这个模型对应数据库
        //with:with的参数可以是字符串,表示单个关联查询,也可以是数组,表示多个关联查询。如果你还额外加了些统计查询,一定不要忘了把with也加上
        //where:表示查询条件
        //order:表示以排序字段$sort作为查询字段(这里是主键id),并以查询规则为$order(例如desc,asc)的顺序进行查询
        //count:表示查询的数据有多少条
        //limit:表示从起始值为$offset开始,每页显示$limit条数据
        //select:表示对数据进行查询
        //具体参数信息见application/common/controller/Backend.php中的buildparams()方法
        $total = $this->model
                ->with(["category"])
                ->where($where)
                ->order($sort, $order)
                ->count();
        $list = $this->model
                ->with(["category"])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
        //将$total的值存入数组中的"total"中,将$list的值存入数组中的"rows"中,并将数组赋值给$result
        $result = array("total" => $total, "rows" => $list);
        //将$result以json的格式返回
        return json($result);
    }
    //表示调用\thinkphp\library\think\view.php的fetch()方法,作用:解析和获取模板内容 用于输出
    return $this->view->fetch();
}

数组使用:

例子:

下方$result为数组信息

public function test()
  {
    $data = Db::table('customers')
      ->where([
        'customer_code' =>['like','吴江'.'%']//当栏位customer_code的值为吴江XXX的值
      ])
      ->column('customer_code');//查询栏位customer_code这一行的数据
    $result = array("total"=>1,"rows"=>$data);//定义数组
    return json($result);//输出结果
  }

返回结果为

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值