middleware('auth'); } public function index() { return view('admin.module-generator'); } public function generate(Request $request) { $request->validate([ 'title' => 'required|string|max:255', 'columns' => 'required|array', 'columns.*.name' => 'required|string', 'columns.*.type' => 'required|string', ]); $title = $request->title; $slug = Str::slug($title); $tableName = str_replace('-', '_', $slug); $modelName = Str::studly($slug); // 1. Create Migration File $timestamp = date('Y_m_d_His'); $migrationFilename = "{$timestamp}_create_{$tableName}_table.php"; $migrationPath = database_path("migrations/{$migrationFilename}"); $migrationContent = "id();\n"; foreach ($request->columns as $column) { $colName = Str::snake($column['name']); $uiType = $column['type']; $dbType = 'string'; switch ($uiType) { case 'integer': $dbType = 'integer'; break; case 'decimal': $dbType = 'decimal'; break; case 'date': $dbType = 'date'; break; case 'datetime': $dbType = 'dateTime'; break; case 'long-text': $dbType = 'longText'; break; case 'boolean': $dbType = 'boolean'; break; default: $dbType = 'string'; break; } if ($dbType == 'decimal') { $migrationContent .= " \$table->decimal('{$colName}', 15, 2)->nullable();\n"; } else { $migrationContent .= " \$table->{$dbType}('{$colName}')->nullable();\n"; } } $migrationContent .= " \$table->timestamps();\n \$table->softDeletes();\n });\n }\n\n public function down()\n {\n Schema::dropIfExists('{$tableName}');\n }\n}\n"; File::put($migrationPath, $migrationContent); // Run the specific migration Artisan::call('migrate', [ '--path' => "database/migrations/{$migrationFilename}", '--force' => true ]); // 2. Create Model $modelPath = app_path("Models/{$modelName}.php"); if (!File::exists($modelPath)) { $modelContent = "columns as $column) { $colName = Str::snake($column['name']); $colType = $column['type']; $relationDatasCode .= " '{$colName}' => [\n 'type' => '{$colType}',\n ],\n"; } $relationDatasCode .= "];"; // Replace the hardcoded relationDatas in template $pattern = '/\$relationDatas = \[.*?\];/s'; $templateContent = preg_replace($pattern, $relationDatasCode, $templateContent); File::put($viewPath, $templateContent); } } // 4. Handle Icon $iconName = $slug; if ($request->hasFile('icon')) { $request->file('icon')->move(base_path('assets/icons'), $iconName . '.png'); } // 5. Insert into types table $type = Types::where('slug', $slug)->first(); if (!$type) { $type = new Types(); $type->title = $title; $type->slug = $slug; $type->icon = $iconName; $type->enabled = 1; $type->full_control = "1"; // Default admin level $type->write = "1"; $type->read = "1"; $type->modify = "1"; $type->save(); } return redirect("admin/types/{$slug}")->with('success', 'Module generated successfully!'); } }