SystemCrudServices.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace app\services\system;
  14. use app\dao\system\SystemCrudDao;
  15. use app\services\BaseServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\crud\Controller;
  18. use crmeb\services\crud\Dao;
  19. use crmeb\services\crud\Make;
  20. use crmeb\services\crud\Model;
  21. use crmeb\services\crud\Route;
  22. use crmeb\services\crud\Service;
  23. use crmeb\services\crud\Validate;
  24. use crmeb\services\crud\ViewApi;
  25. use crmeb\services\crud\ViewPages;
  26. use crmeb\services\crud\ViewRouter;
  27. use crmeb\services\FileService;
  28. use Phinx\Db\Adapter\AdapterFactory;
  29. use think\facade\Db;
  30. use think\helper\Str;
  31. use think\migration\db\Table;
  32. /**
  33. * Class SystemCrudServices
  34. * @author 等风来
  35. * @email 136327134@qq.com
  36. * @date 2023/4/6
  37. * @package app\services\system
  38. */
  39. class SystemCrudServices extends BaseServices
  40. {
  41. //不能生成的系统自带表
  42. const NOT_CRUD_TABANAME = [
  43. 'system_config', 'system_attachment', 'system_attachment_category', 'system_config_tab',
  44. 'system_admin', 'eb_system_city', 'system_log', 'system_menus', 'system_notice',
  45. 'system_notice_admin', 'system_notification', 'system_role', 'system_route',
  46. 'system_route_cate', 'system_storage', 'system_timer', 'system_user_level',
  47. 'system_crud', 'wechat_key', 'user_label_relation', 'user_brokerage_frozen',
  48. 'user_brokerage', 'store_product_cate', 'store_bargain_user_help', 'shipping_templates_region',
  49. 'shipping_templates_no_delivery', 'shipping_templates_free', 'other_order_status', 'lang_code',
  50. 'lang_country', 'app_version', 'user', 'wechat_user', 'template_message', 'store_order', 'other_order',
  51. 'store_order_cart_info', 'store_order_economize', 'store_order_invoice', 'store_order_refund',
  52. 'store_order_status', 'store_pink'
  53. ];
  54. /**
  55. * SystemCrudServices constructor.
  56. * @param SystemCrudDao $dao
  57. */
  58. public function __construct(SystemCrudDao $dao)
  59. {
  60. $this->dao = $dao;
  61. }
  62. /**
  63. * @return array
  64. * @author 等风来
  65. * @email 136327134@qq.com
  66. * @date 2023/4/11
  67. */
  68. public function getList()
  69. {
  70. [$page, $limit] = $this->getPageValue();
  71. $list = $this->dao->selectList([], 'add_time,id,name,table_name,table_comment,table_collation', $page, $limit, 'id desc');
  72. $count = $this->dao->count();
  73. return compact('list', 'count');
  74. }
  75. /**
  76. * 数据库字段类型
  77. * @return \string[][]
  78. * @author 等风来
  79. * @email 136327134@qq.com
  80. * @date 2023/4/11
  81. */
  82. public function getTabelRule()
  83. {
  84. $rule = [
  85. 'varchar' => 'string',
  86. 'int' => 'integer',
  87. 'biginteger' => 'bigint',
  88. 'tinyint' => 'boolean',
  89. ];
  90. return [
  91. 'types' => [
  92. 'varchar',
  93. 'char',
  94. 'text',
  95. 'longtext',
  96. 'tinytext',
  97. 'enum',
  98. 'blob',
  99. 'binary',
  100. 'varbinary',
  101. 'datetime',
  102. 'timestamp',
  103. 'time',
  104. 'date',
  105. 'year',
  106. 'boolean',
  107. 'tinyint',
  108. 'int',
  109. 'decimal',
  110. 'float',
  111. 'json',
  112. 'addTimestamps',
  113. 'addSoftDelete',
  114. ],
  115. 'rule' => $rule
  116. ];
  117. }
  118. /**
  119. * 改变数据库类型
  120. * @param string $type
  121. * @return string
  122. * @author 等风来
  123. * @email 136327134@qq.com
  124. * @date 2023/4/13
  125. */
  126. public function changeTabelRule(string $type)
  127. {
  128. if (!in_array($type, $this->getTabelRule()['types'])) {
  129. throw new AdminException(500047);
  130. }
  131. return $this->getTabelRule()['rule'][$type] ?? $type;
  132. }
  133. /**
  134. * @param string $tableName
  135. * @return mixed
  136. * @author 等风来
  137. * @email 136327134@qq.com
  138. * @date 2023/4/14
  139. */
  140. public function getTableInfo(string $tableName)
  141. {
  142. $sql = 'SELECT * FROM `information_schema`.`TABLES` WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?';
  143. $tableInfo = Db::query($sql, [config('database.connections.mysql.database'), $this->getTableName($tableName)]);
  144. return $tableInfo[0] ?? [];
  145. }
  146. /**
  147. * 获取表字段
  148. * @param string $tableName
  149. * @return mixed
  150. * @author 等风来
  151. * @email 136327134@qq.com
  152. * @date 2023/4/7
  153. */
  154. public function getColumnNamesList(string $tableName)
  155. {
  156. $sql = 'SELECT * FROM `information_schema`.`columns` WHERE TABLE_SCHEMA = ? AND table_name = ? ORDER BY ORDINAL_POSITION';
  157. $column = Db::query($sql, [config('database.connections.mysql.database'), $this->getTableName($tableName)]);
  158. $columns = [];
  159. foreach ($column as $item) {
  160. $column = [
  161. 'name' => $item['COLUMN_NAME'],
  162. 'type' => $item['DATA_TYPE'],
  163. 'dataType' => stripos($item['COLUMN_TYPE'], '(') !== false ? substr_replace($item['COLUMN_TYPE'], '', stripos($item['COLUMN_TYPE'], ')') + 1) : $item['COLUMN_TYPE'],
  164. 'default' => $item['COLUMN_DEFAULT'],
  165. 'null' => $item['IS_NULLABLE'] == 'YES',
  166. 'primaryKey' => $item['COLUMN_KEY'] == 'PRI',
  167. 'unsigned' => (bool)stripos($item['COLUMN_TYPE'], 'unsigned'),
  168. 'autoIncrement' => stripos($item['EXTRA'], 'auto_increment') !== false,
  169. 'comment' => $item['COLUMN_COMMENT'],
  170. 'limit' => $item['CHARACTER_MAXIMUM_LENGTH'] ?: $item['NUMERIC_PRECISION'],
  171. ];
  172. $columns[$item['COLUMN_NAME']] = $column;
  173. }
  174. return $columns;
  175. }
  176. /**
  177. * @param array $data
  178. * @return array
  179. * @author 等风来
  180. * @email 136327134@qq.com
  181. * @date 2023/4/12
  182. */
  183. public function valueReplace(array $data)
  184. {
  185. $replace = ['phar://'];
  186. $newData = [];
  187. foreach ($data as $key => $item) {
  188. if (is_array($item)) {
  189. $item = $this->valueReplace($item);
  190. } else {
  191. $item = str_replace($replace, '', $item);
  192. }
  193. $newData[str_replace($replace, '', $key)] = $item;
  194. }
  195. return $newData;
  196. }
  197. /**
  198. * 创建
  199. * @param array $data
  200. * @return mixed
  201. * @author 等风来
  202. * @email 136327134@qq.com
  203. * @date 2023/4/11
  204. */
  205. public function createCrud(array $data)
  206. {
  207. $tableName = $data['tableName'];
  208. $tableComment = $data['tableComment'] ?? $data['menuName'];
  209. $tableField = $this->valueReplace($data['tableField']);
  210. $filePath = $this->valueReplace($data['filePath']);
  211. $modelName = $data['modelName'] ?? $data['menuName'] ?? $tableName;
  212. if ($this->dao->value(['table_name' => $tableName])) {
  213. throw new AdminException(500048);
  214. }
  215. $data['softDelete'] = false;
  216. //创建数据库
  217. if ($tableField && !$data['isTable']) {
  218. $tableCreateInfo = $this->makeDatebase($tableName, $tableComment, $tableField);
  219. if ($tableCreateInfo['softDelete']) {
  220. $data['softDelete'] = true;
  221. }
  222. }
  223. if (in_array($tableName, self::NOT_CRUD_TABANAME)) {
  224. throw new AdminException(500044);
  225. }
  226. //读取表结构
  227. $column = $this->getColumnNamesList($tableName);
  228. if (!$column) {
  229. throw new AdminException(500049, ['_name' => $tableName]);
  230. }
  231. $tableInfo = $this->getTableInfo($tableName);
  232. //获取主键
  233. foreach ($column as $value) {
  234. if ($value['primaryKey']) {
  235. $data['key'] = $value['name'];
  236. break;
  237. }
  238. }
  239. $routeName = 'crud/' . Str::snake($tableName);
  240. $uniqueAuth = Str::snake($tableName) . '-crud-list-index';
  241. //增加保存的绝对路径
  242. foreach ($filePath as $k => $i) {
  243. if (in_array($k, ['pages', 'router', 'api'])) {
  244. $filePath[$k] = Make::adminTemplatePath() . $i;
  245. } else {
  246. $filePath[$k] = app()->getRootPath() . $i;
  247. }
  248. }
  249. //创建菜单
  250. if (!$data['menuName']) {
  251. $data['menuName'] = $tableName;
  252. }
  253. $dataMenu = [
  254. 'pid' => $data['pid'],
  255. 'menu_name' => $data['menuName'],
  256. 'menu_path' => '/' . $routeName,
  257. 'auth_type' => 1,
  258. 'is_show' => 1,
  259. 'is_del' => 0,
  260. 'unique_auth' => $uniqueAuth,
  261. 'is_header' => $data['pid'] ? 0 : 1,
  262. ];
  263. $res = $this->transaction(function () use ($tableInfo, $modelName, $filePath, $tableName, $routeName, $data, $dataMenu) {
  264. $menuInfo = app()->make(SystemMenusServices::class)->save($dataMenu);
  265. //写入路由权限
  266. $cateId = app()->make(SystemRouteServices::class)->topCateId('adminapi');
  267. $ruleData = [
  268. [
  269. 'path' => $routeName,
  270. 'method' => 'GET',
  271. 'name' => $modelName . '列表接口',
  272. 'app_name' => 'adminapi',
  273. 'cate_id' => $cateId,
  274. 'unique_auth' => '',
  275. 'add_time' => date('Y-m-d H:i:s')
  276. ],
  277. [
  278. 'path' => $routeName . '/create',
  279. 'method' => 'GET',
  280. 'name' => $modelName . '获取创建表单接口',
  281. 'app_name' => 'adminapi',
  282. 'cate_id' => $cateId,
  283. 'unique_auth' => Str::snake($tableName) . '-add',
  284. 'add_time' => date('Y-m-d H:i:s')
  285. ],
  286. [
  287. 'path' => $routeName,
  288. 'method' => 'POST',
  289. 'name' => $modelName . '保存数据接口',
  290. 'app_name' => 'adminapi',
  291. 'cate_id' => $cateId,
  292. 'unique_auth' => '',
  293. 'add_time' => date('Y-m-d H:i:s')
  294. ],
  295. [
  296. 'path' => $routeName . '/<id>/edit',
  297. 'method' => 'GET',
  298. 'name' => $modelName . '获取修改表单接口',
  299. 'app_name' => 'adminapi',
  300. 'cate_id' => $cateId,
  301. 'unique_auth' => '',
  302. 'add_time' => date('Y-m-d H:i:s')
  303. ],
  304. [
  305. 'path' => $routeName . '/<id>',
  306. 'method' => 'PUT',
  307. 'name' => $modelName . '修改数据接口',
  308. 'app_name' => 'adminapi',
  309. 'cate_id' => $cateId,
  310. 'unique_auth' => '',
  311. 'add_time' => date('Y-m-d H:i:s')
  312. ],
  313. [
  314. 'path' => $routeName . '/<id>',
  315. 'method' => 'DELETE',
  316. 'name' => $modelName . '删除数据接口',
  317. 'app_name' => 'adminapi',
  318. 'cate_id' => $cateId,
  319. 'unique_auth' => '',
  320. 'add_time' => date('Y-m-d H:i:s')
  321. ],
  322. ];
  323. app()->make(SystemRouteServices::class)->saveAll($ruleData);
  324. //记录权限加入菜单表
  325. $menuData = [];
  326. foreach ($ruleData as $item) {
  327. $menuData[] = [
  328. 'pid' => $menuInfo->id,
  329. 'methods' => $item['method'],
  330. 'api_url' => $item['path'],
  331. 'unique_auth' => $item['unique_auth'],
  332. 'menu_name' => $item['name'],
  333. 'is_del' => 0,
  334. 'auth_type' => 2,
  335. ];
  336. }
  337. $menus = app()->make(SystemMenusServices::class)->saveAll($menuData);
  338. $menuIds = array_column($menus->toArray(), 'id');
  339. array_push($menuIds, $menuInfo->id);
  340. //生成文件
  341. $make = $this->makeFile($tableName, $routeName, config('app.crud_make', false), $data, $filePath);
  342. $makePath = [];
  343. foreach ($make as $key => $item) {
  344. $makePath[$key] = $item['path'];
  345. }
  346. //记录crud生成
  347. $res = $this->dao->save([
  348. 'pid' => $data['pid'],
  349. 'name' => $data['menuName'],
  350. 'model_name' => $data['modelName'],
  351. 'table_name' => $tableName,
  352. 'table_comment' => $tableInfo['TABLE_COMMENT'] ?? '',
  353. 'table_collation' => $tableInfo['TABLE_COLLATION'] ?? '',
  354. 'field' => json_encode($data),//提交的数据
  355. 'menu_ids' => json_encode($menuIds),//生成的菜单id
  356. 'make_path' => json_encode($makePath),
  357. 'add_time' => time()
  358. ]);
  359. return $res;
  360. });
  361. return $res->toArray();
  362. }
  363. /**
  364. * 获取数据库配置
  365. * @return array
  366. */
  367. protected function getDbConfig(): array
  368. {
  369. $default = app()->config->get('database.default');
  370. $config = app()->config->get("database.connections.{$default}");
  371. if (0 == $config['deploy']) {
  372. $dbConfig = [
  373. 'adapter' => $config['type'],
  374. 'host' => $config['hostname'],
  375. 'name' => $config['database'],
  376. 'user' => $config['username'],
  377. 'pass' => $config['password'],
  378. 'port' => $config['hostport'],
  379. 'charset' => $config['charset'],
  380. 'table_prefix' => $config['prefix'],
  381. ];
  382. } else {
  383. $dbConfig = [
  384. 'adapter' => explode(',', $config['type'])[0],
  385. 'host' => explode(',', $config['hostname'])[0],
  386. 'name' => explode(',', $config['database'])[0],
  387. 'user' => explode(',', $config['username'])[0],
  388. 'pass' => explode(',', $config['password'])[0],
  389. 'port' => explode(',', $config['hostport'])[0],
  390. 'charset' => explode(',', $config['charset'])[0],
  391. 'table_prefix' => explode(',', $config['prefix'])[0],
  392. ];
  393. }
  394. $table = app()->config->get('database.migration_table', 'migrations');
  395. $dbConfig['default_migration_table'] = $dbConfig['table_prefix'] . $table;
  396. return $dbConfig;
  397. }
  398. public function getAdapter()
  399. {
  400. $options = $this->getDbConfig();
  401. $adapter = AdapterFactory::instance()->getAdapter($options['adapter'], $options);
  402. if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
  403. $adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter);
  404. }
  405. return $adapter;
  406. }
  407. /**
  408. * 创建数据库
  409. * @param string $tableName
  410. * @param string $tableComment
  411. * @param array $tableField
  412. * @return array
  413. * @author 等风来
  414. * @email 136327134@qq.com
  415. * @date 2023/4/7
  416. */
  417. public function makeDatebase(string $tableName, string $tableComment, array $tableField = [])
  418. {
  419. $softDelete = false;
  420. $timestamps = false;
  421. $indexField = [];
  422. //创建表
  423. $table = new Table($tableName, ['comment' => $tableComment], $this->getAdapter());
  424. //创建字段
  425. foreach ($tableField as $item) {
  426. $option = [];
  427. if (isset($item['limit'])) {
  428. $option['limit'] = (int)$item['limit'];
  429. }
  430. if (isset($item['default'])) {
  431. $option['default'] = $item['default'];
  432. }
  433. //创建伪删除
  434. if ($item['field_type'] === 'addSoftDelete') {
  435. $table->addSoftDelete();
  436. $softDelete = true;
  437. } else if ($item['field_type'] === 'addTimestamps') {
  438. //创建修改和增加时间
  439. $table->addTimestamps();
  440. $timestamps = true;
  441. } else {
  442. $option['comment'] = $item['comment'];
  443. $table->addColumn($item['field'], $this->changeTabelRule($item['field_type']), $option);
  444. }
  445. }
  446. //创建索引
  447. if (!empty($data['tableIndex'])) {
  448. $indexField = $data['tableIndex'];
  449. foreach ($data['tableIndex'] as $item) {
  450. $table->addIndex($item);
  451. }
  452. }
  453. //执行创建
  454. $table->create();
  455. return compact('indexField', 'softDelete', 'timestamps');
  456. }
  457. /**
  458. * 创建文件返回文件路径和内容
  459. * @param string $tableName
  460. * @param string $routeName
  461. * @param bool $isMake
  462. * @param array $options
  463. * @param array $filePath
  464. * @param string $basePath
  465. * @return array[]
  466. * @author 等风来
  467. * @email 136327134@qq.com
  468. * @date 2023/4/7
  469. */
  470. public function makeFile(string $tableName, string $routeName, bool $isMake = false, array $options = [], array $filePath = [], string $basePath = '')
  471. {
  472. $options['fromField'] = is_array($options['fromField']) ? $options['fromField'] : [];
  473. $options['columnField'] = is_array($options['columnField']) ? $options['columnField'] : [];
  474. //生成模型
  475. $model = app()->make(Model::class);
  476. $model->setFilePathName($filePath['model'] ?? '')->setbasePath($basePath)->handle($tableName, $options);
  477. //生成dao
  478. $dao = app()->make(Dao::class);
  479. $dao->setFilePathName($filePath['dao'] ?? '')->setbasePath($basePath)->handle($tableName, [
  480. 'usePath' => $model->getUsePath(),
  481. ]);
  482. //生成service
  483. $service = app()->make(Service::class);
  484. $service->setFilePathName($filePath['service'] ?? '')->setbasePath($basePath)->handle($tableName, [
  485. 'field' => $options['fromField'],
  486. 'key' => $options['key'],
  487. 'usePath' => $dao->getUsePath(),
  488. 'modelName' => $options['modelName'] ?? '',
  489. ]);
  490. //生成验证器
  491. $validate = app()->make(Validate::class);
  492. $validate->setFilePathName($filePath['validate'] ?? '')->setbasePath($basePath)->handle($tableName, [
  493. 'field' => $options['fromField'],
  494. ]);
  495. //生成控制器
  496. $controller = app()->make(Controller::class);
  497. $controller->setFilePathName($filePath['controller'] ?? '')->setbasePath($basePath)->handle($tableName, [
  498. 'usePath' => $service->getUsePath(),
  499. 'validateName' => '\\' . str_replace('/', '\\', $validate->getUsePath()) . 'Validate::class',
  500. 'field' => array_column($options['fromField'], 'field'),
  501. ]);
  502. //生成路由
  503. $route = app()->make(Route::class);
  504. $route->setFilePathName($filePath['route'] ?? '')->setbasePath($basePath)->handle($tableName, [
  505. 'menus' => $options['modelName'] ?? $options['menuName'],
  506. 'route' => $routeName
  507. ]);
  508. //生成前台路由
  509. $viewRouter = app()->make(ViewRouter::class);
  510. $viewRouter->setFilePathName($filePath['router'] ?? '')->setbasePath($basePath)->handle($tableName, [
  511. 'route' => $routeName,
  512. 'menuName' => $options['menuName'],
  513. ]);
  514. //生成前台接口
  515. $viewApi = app()->make(ViewApi::class);
  516. $viewApi->setFilePathName($filePath['api'] ?? '')->setbasePath($basePath)->handle($tableName, [
  517. 'route' => $routeName,
  518. ]);
  519. //生成前台页面
  520. $viewPages = app()->make(ViewPages::class);
  521. $viewPages->setFilePathName($filePath['pages'] ?? '')->setbasePath($basePath)->handle($tableName, [
  522. 'field' => $options['columnField'],
  523. 'route' => $routeName,
  524. 'key' => $options['key'],
  525. 'pathApiJs' => '@/' . str_replace('\\', '/', str_replace([Make::adminTemplatePath(), '.js'], '', $viewApi->getPath())),
  526. ]);
  527. //创建文件
  528. if ($isMake) {
  529. FileService::batchMakeFiles([$model, $validate, $dao, $service, $controller, $route, $viewApi, $viewPages, $viewRouter]);
  530. }
  531. return [
  532. 'controller' => [
  533. 'path' => $this->replace($controller->getPath()),
  534. 'content' => $controller->getContent()
  535. ],
  536. 'model' => [
  537. 'path' => $this->replace($model->getPath()),
  538. 'content' => $model->getContent()
  539. ],
  540. 'dao' => [
  541. 'path' => $this->replace($dao->getPath()),
  542. 'content' => $dao->getContent()
  543. ],
  544. 'route' => [
  545. 'path' => $this->replace($route->getPath()),
  546. 'content' => $route->getContent()
  547. ],
  548. 'service' => [
  549. 'path' => $this->replace($service->getPath()),
  550. 'content' => $service->getContent()
  551. ],
  552. 'validate' => [
  553. 'path' => $this->replace($validate->getPath()),
  554. 'content' => $validate->getContent()
  555. ],
  556. 'router' => [
  557. 'path' => $this->replace($viewRouter->getPath()),
  558. 'content' => $viewRouter->getContent()
  559. ],
  560. 'api' => [
  561. 'path' => $this->replace($viewApi->getPath()),
  562. 'content' => $viewApi->getContent()
  563. ],
  564. 'pages' => [
  565. 'path' => $this->replace($viewPages->getPath()),
  566. 'content' => $viewPages->getContent()
  567. ],
  568. ];
  569. }
  570. protected function replace(string $path)
  571. {
  572. return str_replace([app()->getRootPath(), Make::adminTemplatePath()], '', $path);
  573. }
  574. /**
  575. * @param string $tableName
  576. * @param bool $fullName
  577. * @return string
  578. * @author 等风来
  579. * @email 136327134@qq.com
  580. * @date 2023/4/7
  581. */
  582. public function getTableName(string $tableName, bool $fullName = true)
  583. {
  584. $tablePrefix = config('database.connections.mysql.prefix');
  585. $pattern = '/^' . $tablePrefix . '/i';
  586. return ($fullName ? $tablePrefix : '') . (preg_replace($pattern, '', $tableName));
  587. }
  588. }