From 0fd541cc439f9caef32e07a164ffde3e3082aada Mon Sep 17 00:00:00 2001 From: Bhavin Gajjar Date: Fri, 29 May 2020 15:54:02 +0530 Subject: [PATCH] resource add colums name in array return --- src/LaravelApiGenerator.php | 31 +++++++++++++++++++------------ src/resources/stubs/resource.stub | 5 ++++- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/LaravelApiGenerator.php b/src/LaravelApiGenerator.php index 949b5f9..4799cef 100644 --- a/src/LaravelApiGenerator.php +++ b/src/LaravelApiGenerator.php @@ -6,7 +6,7 @@ class LaravelApiGenerator { - const STUB_DIR = __DIR__.'/resources/stubs/'; + const STUB_DIR = __DIR__ . '/resources/stubs/'; protected $model; protected $result = false; @@ -23,10 +23,10 @@ public function generate() public function directoryCreate() { - if (! file_exists(base_path('app/Http/Controllers/Api'))) { + if (!file_exists(base_path('app/Http/Controllers/Api'))) { mkdir(base_path('app/Http/Controllers/Api')); } - if (! file_exists(base_path('app/Http/Resources'))) { + if (!file_exists(base_path('app/Http/Resources'))) { mkdir(base_path('app/Http/Resources')); } } @@ -34,12 +34,12 @@ public function directoryCreate() public function generateController() { $this->result = false; - if (! file_exists(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'))) { + if (!file_exists(base_path('app/Http/Controllers/Api/' . $this->model . 'Controller.php'))) { $template = self::getStubContents('controller.stub'); $template = str_replace('{{modelName}}', $this->model, $template); $template = str_replace('{{modelNameLower}}', strtolower($this->model), $template); $template = str_replace('{{modelNameCamel}}', Str::camel($this->model), $template); - file_put_contents(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'), $template); + file_put_contents(base_path('app/Http/Controllers/Api/' . $this->model . 'Controller.php'), $template); $this->result = true; } @@ -49,10 +49,17 @@ public function generateController() public function generateResource() { $this->result = false; - if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Resource.php'))) { + if (!file_exists(base_path('app/Http/Resources/' . $this->model . 'Resource.php'))) { + $model = app("App\\" . $this->model); + $columns = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable()); + $print_columns = null; + foreach ($columns as $key => $column) { + $print_columns .= "'" . $column . "'" . ' => $this->' . $column . ', ' . "\n \t\t\t"; + } $template = self::getStubContents('resource.stub'); $template = str_replace('{{modelName}}', $this->model, $template); - file_put_contents(base_path('app/Http/Resources/'.$this->model.'Resource.php'), $template); + $template = str_replace('{{columns}}', $print_columns, $template); + file_put_contents(base_path('app/Http/Resources/' . $this->model . 'Resource.php'), $template); $this->result = true; } @@ -62,10 +69,10 @@ public function generateResource() public function generateCollection() { $this->result = false; - if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Collection.php'))) { + if (!file_exists(base_path('app/Http/Resources/' . $this->model . 'Collection.php'))) { $template = self::getStubContents('collection.stub'); $template = str_replace('{{modelName}}', $this->model, $template); - file_put_contents(base_path('app/Http/Resources/'.$this->model.'Collection.php'), $template); + file_put_contents(base_path('app/Http/Resources/' . $this->model . 'Collection.php'), $template); $this->result = true; } @@ -75,10 +82,10 @@ public function generateCollection() public function generateRoute() { $this->result = false; - $template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller');"."\n"; + $template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller');" . "\n"; $route = str_replace('{{modelNameLower}}', Str::camel(Str::plural($this->model)), $template); $route = str_replace('{{modelName}}', $this->model, $route); - if (! strpos(file_get_contents(base_path('routes/api.php')), $route)) { + if (!strpos(file_get_contents(base_path('routes/api.php')), $route)) { file_put_contents(base_path('routes/api.php'), $route, FILE_APPEND); $this->result = true; } @@ -88,6 +95,6 @@ public function generateRoute() private function getStubContents($stubName) { - return file_get_contents(self::STUB_DIR.$stubName); + return file_get_contents(self::STUB_DIR . $stubName); } } diff --git a/src/resources/stubs/resource.stub b/src/resources/stubs/resource.stub index 9b5b38c..226e83b 100644 --- a/src/resources/stubs/resource.stub +++ b/src/resources/stubs/resource.stub @@ -30,7 +30,10 @@ class {{modelName}}Resource extends JsonResource */ public function toArray($request) { - return parent::toArray($request); + //return parent::toArray($request); + return [ + {{columns}} + ]; } /**