Browse Source

fixed:后台-公共列表视图-添加模型兼容

刘学玺 3 months ago
parent
commit
e78d00bb4c
1 changed files with 9 additions and 2 deletions
  1. 9 2
      app/Services/ViewService.php

+ 9 - 2
app/Services/ViewService.php

@@ -21,7 +21,15 @@ class ViewService
     public function viewList($viewName, array $whereParams, array $pageParams, array $sortParams = []): array
     {
         try {
-            $query = DB::table($viewName);
+            // 尝试获取对应的模型类名
+            $modelClass = 'App\\Models\\' . str_replace(' ', '', ucwords(str_replace('_', ' ', $viewName)));
+
+            // 检查模型是否存在
+            if (class_exists($modelClass)) {
+                $query = app($modelClass)->query();
+            } else {
+                $query = DB::table($viewName);
+            }
 
             // 条件筛选
             foreach ($whereParams as $key => $value) {
@@ -47,7 +55,6 @@ class ViewService
                 'msg' => 'success',
                 'status' => 0,
             ];
-
         } catch (\Exception $e) {
             Log::error('获取视图数据失败', [
                 'view_name' => $viewName,