From 2758059bcf83d70cd9822992dab3583328806bcf Mon Sep 17 00:00:00 2001 From: ai567 <978713427@qq.com> Date: Thu, 12 Dec 2024 17:08:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BC=98=E5=8C=96LaravelORMFactory?= =?UTF-8?q?=E7=9A=84count=E6=96=B9=E6=B3=95=E6=9F=A5=E8=AF=A2=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- madong/factories/LaravelORMFactory.php | 35 +++++++++++++++----------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/madong/factories/LaravelORMFactory.php b/madong/factories/LaravelORMFactory.php index 7c5f2d7..7a25da1 100644 --- a/madong/factories/LaravelORMFactory.php +++ b/madong/factories/LaravelORMFactory.php @@ -16,23 +16,30 @@ class LaravelORMFactory { $this->model = $model; } - /** - * 获取条数 - * - * @param array $where - * @param bool $search - * - * @return int - * @throws \Exception - */ - public function count(array $where = [], bool $search = true): int - { - return $this->getModel()->when($search, function ($query) use ($where) { - $query->where($where); - })->count(); + * 获取条数 + * + * @param array $where + * @param bool $search + * + * @return int + * @throws \Exception + */ + public function count(array $where = [], bool $search = false): int + { + // 获取查询构建器实例 + $query = $this->getModel()->query(); + if ($search) { + $query = $this->search($where); // search 返回的是一个查询构建器 + } else { + $query->where($where); // 应用 where 条件 + } + + // 返回满足条件的记录数量 + return $query->count(); } + /** * 查询列表 * -- Gitee