fastadmin 关联预载入搜索查询

fastadmin 关联预载入搜索查询_第1张图片
//关联user模型

 public function user()
    {
        return $this->belongsTo('User','user_id','id',[],'LEFT')->setEagerlyType(0);
    }

//控制器打开关联搜索
protected $relationSearch = true;

//控制器

 public function index()
    {
//        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with('user')
                ->where($where)
                ->order($sort, $order)
                ->count();


            $list = $this->model
                ->with('user')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

//js

 table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.username', title: __('用户名')},
                        {field: 'title', title: __('Title'),operate:false},
                        {field: 'images', title: __('Images'), events: Table.api.events.image, formatter: Table.api.formatter.images,operate:false},
                        {field: 'enclosure', title: __('链接地址'),formatter:function($value){ return "+$value+">"+$value+""},operate:false},
                        {field: 'view', title: __('View'),operate:false},
                        {field: 'zan', title: __('Zan'),operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

你可能感兴趣的:(fastadmin 关联预载入搜索查询)