$sql = Db::connect('數據庫')->table('C表')->where('status', 'normal')->field('字段 別稱【不可與其他表重復】')->buildSql(); $total = $this->model ->fetchSql(false) ->with(['B表']) ->join([$sql=>'C表'],'C表關聯B表字段 = B表關聯C表字段','left') ->where($where) ->order($sort, $order) ->count();
上面是自己想的,后來發現了下面這種方法更簡單
->with(['B模型'=>['C方法']])
foreach ($list as $row) {
$row->visible(['B模型']);
$row->getRelation('B模型')->visible(['C方法']);
$row->seller = ['C字段'=>$row->B模型->C方法->C字段];
}