For installation instructions please follow step by step in Quick Start page
Generator provides various commands to generate scaffold & apis.
It is really important how to use generator. Generator supports three methods of input:
We will discuss console method here. You can check other options from above links.
When you run command, it give following instructions,
Here, you have to insert fields for you models, except id & timestamps (created_at & updated_at) fields.
That will be automatically added by generator. When you are done with inserting fields. Type "exit".
When you run any command, it asks for two things:
Now, let's get started with specifying the field. There is some format in which we have to specify field inputs
name db_type html_type options
Here is what this means,
name
- name of the field (snake_case recommended)db_type
- database type. e.g.
$table->string('field_name')
$table->string('field_name', 25)
$table->text('field_name')
$table->integer('field_name',false,true)
$table->string('field_name')->unique()
$table->foreign('field_name')->references('id')->on('table_name')
html_type
- html field type for forms. e.g.
Here is the full guide for field inputs
Here are all options by which you can prevent it, these all fields should be passed by comma separated string.
e.g. s,f,if,ii
so here are some examples, how field inputs can be passed together
title string text
body text textarea s,ii
email string:unique email
writer_id integer:unsigned:foreign,writers,id text s
In second field you can specify validations for fields from any available validations of laravel. e.g.
Generator also supports various other commands to generate files individually like generating an only model, repository or controller etc. You can find a full doc here.
You can configure paths, namespaces, options, add_ons from published config file (config\infyom\laravel_generator.php).
migration
- path where migration file should be generated
model
- path where the model file should be generated
datatables
- path where DataTable files should be generated
repository
- path where repository file should be generated
routes
- path of routes file where routes should be added
api_routes
- path of api_routes.php (this file will contain all API routes)
request
- path where request file should be generated
api_request
- path where API request file should be generated
controller
- path where scaffold controller file should be generated
api_controller
- path where API controller file should be generated
test_trait
- path where test trait file should be generated
repository_test
- path where repository test file should be generated
api_test
- path where test files should be generated
api_controller
- path where API controller file should be generated
test_trait
- path where test trait file should be generated
repository_test
- path where repository test file should be generated
api_test
- path where test files should be generated
views
- path where views file should be generated
schema_files
- path where all schema files should be stored
templates_dir
- path where all templates should be published
model
- Namespace of Model
datatables
- Namespace of DataTable files
repository
- Namespace of Repository
controller
- Namespace of scaffold Controller
api_controller
- Namespace of API Controller
request
- Namespace of scaffold Request
api_request
- Namespace of API Request
templates
- Scaffold Templates types (core-templates, adminlte-templates, metronic-templates
or flatlab-templates)
model_extend_class
- Model Extends Class
api_prefix
- API prefix
api_version
- API version
softDelete
- use soft delete with models
route
- route prefix
path
- path prefix
view
- view prefix
public
- public folder prefix
swagger
- generate swagger annotations for APIs
tests
- generate test cases for APIs
datatables
- generate CRUD index file with datatables
menu
- If you are using generator's default layout then make it true to generate sidebar menu for module
enabled
- enable timestamps
created_at
- Created At timestamp field name
updated_at
- Updated At timestamp field name
deleted_at
- Deleted At timestamp field name
Models can be generated with soft delete option. It is by default
false
.
You can configure it from config
(config/infyom/laravel_generator.php)
. In order to enable it,
set
'options'
=
>
'softDelete'
=
>
true
.
If you want custom delete_at
column name then you can specify in
config/infyom/laravel_generator.php
at
'timestamps'
=
>
'deleted_at'
=
>
'custom_deleted_at'
.
You can also generate swagger annotations for your apis. swagger option is false by default and can be enabled from config.
set :
'add_on.swagger'
=
>
true
Also, in addition, you need to configure jlapp/swaggervel. You can find installation process here
You can also generate test cases for your APIs, test generation option is false by default and can be enabled from config.
Here are the full steps to generate test cases:
'add_on.tests' => true
in
config/laravel_generator.php
.
config/laravel_generator.php
=> 'paths' =>
'api_tests'
=> {your_test_directory
).
Otherwise you can just skip this step.
php artisan infyom.publish:tests
. It will
generate a ApiTestTrait.php
into your tests directory.
composer.json
,composer dump-autoload
The Generator has various commands to generate files in a group or individually. Sometimes you need to generate/re-generate individual files after making some manual changes to some files and you don't want the generator to override your changes. Also, it has command to rollback all generated files.
To rollback, we have the following command,
Where,
{ModelName} - Its a model name for which you want to delete files
{COMMAND_TYPE} - Command type from api, scaffold or api_scaffold
To paginate records, you can use --paginate option to command. e.g.
The Generator also has the ability to generate CRUD file with datatables.
To enable datatables option, you need to install yajra/laravel-datatables package.
You can find full installation steps here.
Make sure you install right datatables version based on its compatibility.
Following versions are supported by Generator:
Generator/Laravel Version | DataTables Version |
---|---|
5.1 | 6.x |
5.2 | 6.x |
5.3 | 6.x |
5.4 | 6.x |
5.5 | 8.x |
You need to install Datatables and Buttons plugin as well, if you are using version 8.x.
Datatables can be configured from config. Once, you are done with installation.
Make 'add_on.datatables' => true in config/webcore/laravel_generator.php.
But Generator also gives option to override the option on the fly while generating files.
You can specify --datatables=true
OR --datatables=false
to override configured value of Datatable.
While generating views, if you don't want to generate all views again. Then you can pass an option --views. For example,
You can specify any of these values and only that view files will be generated.
php artisan generate:scaffold {ModelName} --views=index,create,edit,show
The Generator also has an option to save model schema to json file, so it can be used in future and don't need to re-enter the whole schema from the console.
Schema folder can be configured in config. set
'path' => 'schema_files' => 'folder_path'.
Pass --save
option to command to save file,
If you have schema files stored then it can be used with generator rather than entering schema from the console.
To use schema file, use --fieldsFile option.
Here is the definition and possible options for schema field:
You can also define relationship in field as following:
Some relationships, like One to Many do not have a local field in current model, but some other model contains its primary key as foreign key. In such cases, you can define relationship by following definition:
You can also specify your own custom table name by
By default, Generator takes the primary key as id field. But is also gives you the flexibility to use your own primary key field name via --primary option.
Sometimes, you don't want to directly generate the files into configured folder but in a subfolder of it. Like, admin and that subfolder should be created with namespaces in all generated files. Then you can use --prefix=admin option.
The Generator also gives the flexibility to choose what you want to generate or what you want to skip. While using generator command, you can specify skip option to skip files which will not be generated.
You can specify any file from the following list:
While passing htmlType in a field, we have different syntax for different fields. Here is the full guide for each supported field.
Supported HTML Input Types & Formats | Valid Examples |
---|---|
text | text |
textarea | textarea |
date | date |
number | number |
password | password |
file (partially supported) | file |
select select,value1,value2,value3 select,label1:value1,label2:value2,label3:value3 |
select,Daily,Weekly,Monthly select,Sunday:0,Monday:1,Tuesday:2 |
checkbox checkbox,value |
checkbox checkbox,yes checkbox,1 |
radio radio,label1,label2 radio,label1:value1,label2:value2 |
radio,Male,Female radio,Yes:1,No:0 |
API Generator has a bunch of options and parameters. Following parameters are supported with index API.