CrudController.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * 创建增删改查
  4. * User: Mike
  5. * Email: m@9026.com
  6. * Date: 2016/6/4
  7. * Time: 17:14
  8. */
  9. namespace App\Http\Controllers\Admin\Base;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Services\CRUD\CRUD;
  12. use Illuminate\Support\Facades\DB;
  13. use App\Services\Admin\Menus;
  14. use Request;
  15. class CrudController extends Controller
  16. {
  17. function create() {
  18. if(Request::method() == 'POST') {
  19. return $this->_createSave();
  20. }
  21. $schema = env("DB_DATABASE");
  22. $tables = DB::select("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA='$schema'");
  23. $MenusTrees = (new Menus())->getMenusTree();
  24. return view('admin.base.crud.create',compact('tables','MenusTrees'));
  25. }
  26. private function _createSave() {
  27. $data = Request::all();
  28. $obj = new CRUD();
  29. $ok = $obj->create($data);
  30. if($ok) {
  31. $this->showMessage("操作成功");
  32. }else{
  33. $this->showWarning("操作失败:" . $obj->getMessage());
  34. }
  35. }
  36. public function getTable(){
  37. $table = request()->get('table');
  38. $schema = env("DB_DATABASE");
  39. $data = DB::select("SELECT COLUMN_NAME,COLUMN_KEY FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='{$schema}' AND TABLE_NAME='{$table}';");
  40. }
  41. public function checkModelPath($modelPath = null) {
  42. if(!$modelPath) {
  43. $modelPath = request()->get('path');
  44. }
  45. $modelPath = app_path() . "/Models/" . $modelPath . ".php";
  46. if(file_exists($modelPath)) {
  47. return json_encode(['status'=>FAILURE_CODE,'msg'=>'注意!!Model文件已存在,系统重复不会创建!!!']);
  48. }else{
  49. return json_encode(['status'=>SUCESS_CODE]);
  50. }
  51. }
  52. public function checkServicePath($modelPath = null) {
  53. if(!$modelPath) {
  54. $modelPath = request()->get('path');
  55. }
  56. $modelPath = app_path() . "/Repositories/" . $modelPath . ".php";
  57. if(file_exists($modelPath)) {
  58. return json_encode(['status'=>FAILURE_CODE,'msg'=>'注意!!Service文件已存在,系统重复不会创建!!!']);
  59. }else{
  60. return json_encode(['status'=>SUCESS_CODE]);
  61. }
  62. }
  63. public function checkControllerPath($modelPath = null) {
  64. if(!$modelPath) {
  65. $modelPath = request()->get('path');
  66. }
  67. $modelPath = app_path() . "/Http/Controllers/Admin/" . $modelPath . ".php";
  68. if(file_exists($modelPath)) {
  69. return json_encode(['status'=>FAILURE_CODE,'msg'=>'注意!!Controller文件已存在,系统重复不会创建!!!']);
  70. }else{
  71. return json_encode(['status'=>SUCESS_CODE]);
  72. }
  73. }
  74. public function checkPath($modelPath = null) {
  75. if(!$modelPath) {
  76. $modelPath = request()->get('path');
  77. }
  78. $modelPath = app_path() . "/Http/Controllers/Admin/" . $modelPath . ".php";
  79. if(file_exists($modelPath)) {
  80. return json_encode(['status'=>FAILURE_CODE,'msg'=>'注意!!Controller文件已存在,系统重复不会创建!!!']);
  81. }else{
  82. return json_encode(['status'=>SUCESS_CODE]);
  83. }
  84. }
  85. }