Skip to content

Commit

Permalink
Fix bugs & change docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sohelamin committed Oct 9, 2017
1 parent 6fa9fea commit 052592b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
29 changes: 29 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,33 @@
| `--fields` | The field names for the form. e.g. ```--fields='title#string; content#text``` |
| `--locales` | Locales language type. e.g. locals=en |

### API CRUD Options:

| Option | Description |
| --- | --- |
| `--fields` | The field names for the form. e.g. ```--fields='title#string; content#text; category#select#options={"technology": "Technology", "tips": "Tips", "health": "Health"}; user_id#integer#unsigned'``` |
| `--fields_from_file` | Fields from a JSON file. e.g. `--fields_from_file="/path/to/fields.json"` |
| `--validations` | Validation rules for the fields "col_name#rules_set" e.g. ` "title#min:10|max:30|required" ` - See https://laravel.com/docs/master/validation#available-validation-rules |
| `--controller-namespace` | The namespace of the controller - sub directories will be created |
| `--model-namespace` | The namespace that the model will be placed in - directories will be created |
| `--pk` | The name of the primary key |
| `--pagination` | The amount of models per page for index pages |
| `--indexes` | The fields to add an index to. append "#unique" to a field name to add a unique index. Create composite fields by separating fieldnames with a pipe (` --indexes="title,field1|field2#unique" ` will create normal index on title, and unique composite on fld1 and fld2) |
| `--foreign-keys` | Any foreign keys for the table. e.g. `--foreign-keys="user_id#id#users#cascade"` where user_id is the column name, id is the name of the field on the foreign table, users is the name of the foreign table, and cascade is the operation 'ON DELETE' together with 'ON UPDATE' |
| `--relationships` | The relationships for the model. e.g. `--relationships="comments#hasMany#App\Comment"` in the format |
| `--route` | Include Crud route to routes.php? yes or no |
| `--route-group` | Prefix of the route group |

### API Controller Options:

| Option | Description |
| --- | --- |
| `--crud-name` | The name of the crud. e.g. ```--crud-name="post"``` |
| `--model-name` | The name of the model. e.g. ```--model-name="Post"``` |
| `--model-namespace` | The namespace of the model. e.g. ```--model-namespace="Custom\Namespace\Post"``` |
| `--controller-namespace` | The namespace of the controller. e.g. ```--controller-namespace="Http\Controllers\Client"``` |
| `--validations` | Validation rules for the fields "col_name#rules_set" e.g. ``` "title#min:10|max:30|required" ``` - See https://laravel.com/docs/master/validation#available-validation-rules |
| `--pagination` | The amount of models per page for index pages |
| `--force` | Overwrite already existing controller. |

[← Back to index](README.md)
14 changes: 14 additions & 0 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,18 @@ If you chose not to add the crud route in automatically (see above), you will ne
Route::resource('posts', 'PostsController');
```

### API Commands

For Api Crud

```
php artisan crud:api Posts --fields='title#string; content#text' --controller-namespace=Api
```

For api controller

```
php artisan crud:api-controller Api\\PostsController --crud-name=posts --model-name=Post
```

[← Back to index](README.md)
6 changes: 3 additions & 3 deletions src/Commands/CrudApiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CrudApiCommand extends Command
*
* @var string
*/
protected $signature = 'crud:generate
protected $signature = 'crud:api
{name : The name of the Crud.}
{--fields= : Field names for the form & migration.}
{--fields_from_file= : Fields from a json file.}
Expand All @@ -32,7 +32,7 @@ class CrudApiCommand extends Command
*
* @var string
*/
protected $description = 'Generate Crud including controller, model, views & migrations.';
protected $description = 'Generate api crud including controller, model & migrations.';

/** @var string */
protected $routeName = '';
Expand Down Expand Up @@ -105,7 +105,7 @@ public function handle()
$validations = $this->processJSONValidations($this->option('fields_from_file'));
}

$this->call('crud:apicontroller', ['name' => $controllerNamespace . $name . 'Controller', '--crud-name' => $name, '--model-name' => $modelName, '--model-namespace' => $modelNamespace, '--pagination' => $perPage, '--validations' => $validations]);
$this->call('crud:api-controller', ['name' => $controllerNamespace . $name . 'Controller', '--crud-name' => $name, '--model-name' => $modelName, '--model-namespace' => $modelNamespace, '--pagination' => $perPage, '--validations' => $validations]);
$this->call('crud:model', ['name' => $modelNamespace . $modelName, '--fillable' => $fillable, '--table' => $tableName, '--pk' => $primaryKey, '--relationships' => $relationships]);
$this->call('crud:migration', ['name' => $migrationName, '--schema' => $fields, '--pk' => $primaryKey, '--indexes' => $indexes, '--foreign-keys' => $foreignKeys]);

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/CrudApiControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CrudApiControllerCommand extends GeneratorCommand
*
* @var string
*/
protected $signature = 'crud:apicontroller
protected $signature = 'crud:api-controller
{name : The name of the controler.}
{--crud-name= : The name of the Crud.}
{--model-name= : The name of the Model.}
Expand Down
2 changes: 1 addition & 1 deletion src/CrudGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function register()
'Appzcoder\CrudGenerator\Commands\CrudViewCommand',
'Appzcoder\CrudGenerator\Commands\CrudLangCommand',
'Appzcoder\CrudGenerator\Commands\CrudApiCommand',
'Appzcoder\CrudGenerator\Commands\CrudApiControllerCommand',
'Appzcoder\CrudGenerator\Commands\CrudApiControllerCommand'
);
}
}
8 changes: 4 additions & 4 deletions src/stubs/api-controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DummyClass extends Controller
{
${{crudName}} = {{modelName}}::paginate({{pagination}});

return ${{crudName}}
return ${{crudName}};
}

/**
Expand All @@ -34,7 +34,7 @@ class DummyClass extends Controller
{{validationRules}}
${{crudNameSingular}} = {{modelName}}::create($request->all());

return ${{crudNameSingular}};
return response()->json(${{crudNameSingular}}, 201);
}

/**
Expand Down Expand Up @@ -65,7 +65,7 @@ class DummyClass extends Controller
${{crudNameSingular}} = {{modelName}}::findOrFail($id);
${{crudNameSingular}}->update($request->all());

return ${{crudNameSingular}};
return response()->json(${{crudNameSingular}}, 200);
}

/**
Expand All @@ -79,6 +79,6 @@ class DummyClass extends Controller
{
{{modelName}}::destroy($id);

return 204;
return response()->json(null, 204);
}
}

0 comments on commit 052592b

Please sign in to comment.