argument('module')); $name = Str::studly($this->argument('name')); $pluralName = Str::pluralStudly($name); $snakePlural = Str::snake($pluralName); $basePath = base_path("Modules/{$module}"); $paths = [ 'model' => "{$basePath}/app/Models/{$name}.php", 'controller' => "{$basePath}/app/Http/Controllers/API/{$name}Controller.php", 'storeRequest' => "{$basePath}/app/Http/Requests/{$name}/{$name}StoreRequest.php", 'updateRequest' => "{$basePath}/app/Http/Requests/{$name}/{$name}UpdateRequest.php", 'repository' => "{$basePath}/Repositories/{$name}Repository.php", 'migration' => "{$basePath}/Database/Migrations/".date('Y_m_d_His')."_create_{$snakePlural}_table.php", 'route' => "{$basePath}/routes/api.php", ]; // Make directories foreach ($paths as $path) { File::ensureDirectoryExists(dirname($path)); } // Generate Model with Fillable Template File::put($paths['model'], "responseSuccess(\$this->repo->getAll(request()->all()),'{$name} has been fetched successfully.'); } catch (Exception \$e) { return \$this->responseError([], \$e->getMessage()); } } public function store({$name}StoreRequest \$request): JsonResponse { try { return \$this->responseSuccess(\$this->repo->create(\$request->all()),'{$name} has been created successfully.'); } catch (\Illuminate\Database\QueryException \$exception) { return \$this->responseError([], 'Database error: '.\$exception->getMessage()); } catch (Exception \$e) { return \$this->responseError([], \$e->getMessage()); } } public function show(\$id): JsonResponse { try { return \$this->responseSuccess(\$this->repo->getById(\$id), '{$name} has been fetched successfully.'); } catch (Exception \$e) { return \$this->responseError([], \$e->getMessage()); } } public function update({$name}UpdateRequest \$request, \$id): JsonResponse { try { return \$this->responseSuccess(\$this->repo->update(\$id, \$request->all()),'{$name} has been updated successfully.'); } catch (Exception \$e) { return \$this->responseError([], \$e->getMessage()); } } public function destroy(\$id): JsonResponse { try { return \$this->responseSuccess(\$this->repo->delete(\$id),'{$name} has been deleted successfully.'); } catch (Exception \$e) { return \$this->responseError([], \$e->getMessage()); } } }"); // Generate Migration Content File::put($paths['migration'], "id(); \$table->unsignedBigInteger('restaurant_id')->nullable(); \$table->string('name'); \$table->smallInteger('status')->default(1)->comment('1=Active, 2=InActive'); \$table->timestamps(); \$table->softDeletes(); }); } public function down(): void { Schema::dropIfExists('{$snakePlural}'); } }; "); // Request classes File::put($paths['storeRequest'], " 10, 'search' => '', 'orderBy' => 'id', 'order' => 'desc', 'with_deleted' => false, ]; return array_merge(\$defaultArgs, \$filterData); } private function get{$name}Query(): Builder { return \$this->getQuery() ->select( \"{\$this->table}.id\", \"{\$this->table}.restaurant_id\", \"{\$this->table}.name\", \"{\$this->table}.status\", \"{\$this->table}.created_at\", \"{\$this->table}.deleted_at\" ); } protected function filterSearchQuery(Builder|EloquentBuilder \$query, string \$searchedText): Builder { \$searchable = \"%\$searchedText%\"; return \$query->where(\"{\$this->table}.name\", 'LIKE', \$searchable) ->orWhere(\"{\$this->table}.status\", 'LIKE', \$searchable); } public function getAll(array \$filterData = []): Paginator { \$filter = \$this->getFilterData(\$filterData); \$query = \$this->get{$name}Query(); if (! \$filter['with_deleted']) { \$query->whereNull(\"{\$this->table}.deleted_at\"); } if (!empty(\$filter['search'])) { \$query = \$this->filterSearchQuery(\$query, \$filter['search']); } return \$query ->orderBy(\$filter['orderBy'], \$filter['order']) ->paginate(\$filter['perPage']); } public function getCount(array \$filterData = []): int { \$filter = \$this->getFilterData(\$filterData); \$query = \$this->getQuery(); if (! \$filter['with_deleted']) { \$query->whereNull(\"{\$this->table}.deleted_at\"); } return \$query->count(); } /** * @throws Exception */ public function getByColumn(string \$columnName, \$columnValue, array \$selects = ['*']): ?object { \$item = \$this->get{$name}Query() ->where(\$columnName, \$columnValue) ->first(\$selects); if (empty(\$item)) { throw new Exception( \$this->getExceptionMessage(static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE), Response::HTTP_NOT_FOUND ); } return \$item; } /** * @throws Exception */ public function create(array \$data): object { \$data = \$this->prepareForDB(\$data); \$id = \$this->getQuery()->insertGetId(\$data); return {$name}::find(\$id); } /** * @throws Exception */ public function update(int \$id, array \$data): object { \$item = {$name}::findOrFail(\$id); \$data = \$this->prepareForDB(\$data, \$item); parent::update(\$id, \$data); return \$this->getById(\$id); } /** * @throws Exception */ public function prepareForDB(array \$data, ?object \$item = null): array { \$data = parent::prepareForDB(\$data, \$item); if (empty(\$item)) { \$data['created_at'] = now(); \$data['restaurant_id'] = \$this->getCurrentRestaurantId(); \$data['status'] = 1; if (!empty(\$data['image']) && \$data['image'] instanceof \Illuminate\Http\UploadedFile) { \$data['image'] = fileUploader('{$module}/', 'png', \$data['image']); } } else { if (!empty(\$data['image']) && \$data['image'] instanceof \Illuminate\Http\UploadedFile) { \$data['image'] = fileUploader('{$module}/', 'png', \$data['image'], \$item->image); } \$data['updated_at'] = now(); } return \$data; } protected function getExceptionMessages(): array { return [ static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE => '{$name} does not exist.', static::MESSAGE_ITEM_COULD_NOT_BE_DELETED => '{$name} could not be deleted.', ]; } } "); // Create module route if (! File::exists($paths['route'])) { File::put($paths['route'], "info("✅ {$name} entity created under module {$module} with full structure!"); } }