TP5 模型关联查询并显示

//model
namespace app\admin\model;
use think\Model;
class Product extends Model{
    protected $pk = 'id';
    public function proclass(){
        //return $this->hasOne('Proclass','id','classid')->bind('typename');
        return $this->hasOne('Proclass','id','classid')->field('id,typename');
    }   
}
//controller
namespace app\admin\controller;
use think\Controller;
class Product extends Controller {
    function lists(){
        $list = model('product')->with('proclass')->order('id desc')->select();
        return json(['code' => 0, 'data' => $list]);
    }
}
//layui
...
table.render({
    elem: '#demo',
    url: '{:url("lists")}',
    cols:[[
        {field:id,title:'ID',width:60},
        {field:title,title:'标题',width:200},
        {field:typename,title:'类别',width:200,templet:function(d){
            //retutn d.typename;
            retutn d.proclass.typename;
        }},
        {field:addtime,title:'发布时间',width:120},
        {title:'操作',width:200,toolbar:'#barTpl'}
    ]]
});

 

你可能感兴趣的:(web)