Procházet zdrojové kódy

修复表格字段无法使用模型访问器问题

jqh před 4 roky
rodič
revize
ddd0290d9b
2 změnil soubory, kde provedl 21 přidání a 13 odebrání
  1. 1 13
      src/Grid/Column.php
  2. 20 0
      src/Support/Helper.php

+ 1 - 13
src/Grid/Column.php

@@ -574,21 +574,9 @@ class Column
             return $row;
         }
 
-        // 这里禁止把驼峰转化为下划线
-        if (! empty($row::$snakeAttributes)) {
-            $shouldSnakeAttributes = true;
-
-            $row::$snakeAttributes = false;
-        }
-
         $array = $row->toArray();
 
-        // 转为数组后还原
-        if (isset($shouldSnakeAttributes)) {
-            $row::$snakeAttributes = true;
-        }
-
-        return $array;
+        return Helper::camelArray($array);
     }
 
     /**

+ 20 - 0
src/Support/Helper.php

@@ -906,4 +906,24 @@ class Helper
 
         return $array;
     }
+
+    /**
+     * 把下划线风格字段名转化为驼峰风格.
+     *
+     * @param array $array
+     *
+     * @return array
+     */
+    public static function camelArray(array &$array)
+    {
+        foreach ($array as $k => $v) {
+            if (is_array($v)) {
+                Helper::camelArray($v);
+            }
+
+            $array[Str::camel($k)] = $v;
+        }
+
+        return $array;
+    }
 }