Skip to content

Commit

Permalink
resource add colums name in array return
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavingajjar committed May 29, 2020
1 parent 3fd12fc commit 0fd541c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
31 changes: 19 additions & 12 deletions src/LaravelApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class LaravelApiGenerator
{
const STUB_DIR = __DIR__.'/resources/stubs/';
const STUB_DIR = __DIR__ . '/resources/stubs/';
protected $model;
protected $result = false;

Expand All @@ -23,23 +23,23 @@ 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'));
}
}

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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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);
}
}
5 changes: 4 additions & 1 deletion src/resources/stubs/resource.stub
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class {{modelName}}Resource extends JsonResource
*/
public function toArray($request)
{
return parent::toArray($request);
//return parent::toArray($request);
return [
{{columns}}
];
}

/**
Expand Down

0 comments on commit 0fd541c

Please sign in to comment.