報錯原因
在開啟軟刪除后,設置了表別名,軟刪除字段依舊使用原表名。
解決方法
原代碼
$list = $this->model->with(['admin', 'product'])->where($where)->order($sort, $order)->paginate($limit);foreach ($list as $row) {$row->getRelation('admin')->visible(['nickname']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
替換代碼
$query = $this->model->with(['admin', 'product'])->where($where)->order($sort, $order)->group('supplier.id');
// 克隆查詢對象避免影響后續操作
$countQuery = clone $query;
$listCount = $countQuery->count('supplier.id');
// 然后原來的查詢繼續執行獲取數據
$list = $query->order($sort, $order)->limit($offset, $limit)->select();foreach ($list as $row) {$row->getRelation('admin')->visible(['nickname']);
}$result = array("total" => $listCount, "rows" => $list);