Generator Options
TABLE OF CONTENT
API Generator Options
Supported API Params
API Generator has a bunch of options and parameters. Following parameters are supported with index API.
- limit - Limit the number of returned results
- search - Global search on all searchable fields
- offset - Offset from where results should be returned
- sortedBy - Sort result by given field
Swagger
You can also generate swagger annotations for your apis.
For that, you need to install two packages. Add the following code to your compsoer.json
"repositories": [
{
"type": "vcs",
"url": "git@github.com:InfyOmLabs/swaggervel.git"
}
],
"require": {
"appointer/swaggervel": "dev-master",
"infyomlabs/swagger-generator": "dev-master",
},
After that run a command,
composer update
After that run a command,
php artisan vendor:publish --provider="Appointer\Swaggervel\SwaggervelServiceProvider"
swagger option is false by default and can be enabled from config. set
'add_on.swagger' => true
And we are done. Generate a CRUD for any model and then try to access /api/docs
and you should be able to access the swagger ui.
Test Cases
Test cases for your APIs will be also generated when test generation option is true which is a default value in config. You can make it false, if you do not want to generate it.
'add_on.tests' => false
Scaffold Options
Paginate Records
To paginate records, you can use --paginate
option to command. e.g.
php artisan infyom:api $MODEL_NAME --paginate=10
Datatables
The generator also have ability to generate CRUD file with datatables. To enable datatables option, you need to install yajra/laravel-datatables package. You can find a 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.x | 6.x |
5.2.x | 6.x |
5.3.x | 6.x |
5.4.x | 7.x, 8.x |
5.5.x | 8.x |
5.6.x | 8.x |
5.7.x | 8.x |
5.8.x | 9.x |
6.x.x | 9.x |
7.x.x | 9.x |
You need to install Datatables. If you are using version 8.x or later install Buttons plugin and HTML plugin as well.
Once, you are done with installation. Make 'addon.datatables' => true in `config/infyom/laravelgenerator.php`.
Datatables from CLI
Datatables can be configured from config 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.
php artisan infyom:scaffold $MODEL_NAME --datatables=true
Generate Only Specified Views
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 infyom:scaffold $MODEL_NAME --views=index,create,edit,show
Generator Options
Soft Delete
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' => 'deletedat' => 'customdeleted_at'.
Timestamps
If you don't want generate timestamps fields (createdat, updatedat) then you can use this option by changing, 'options' => 'timestamps' => false
Save model schema
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.
It's enabled by default, in the configuration file in 'options' => 'saveschemafile' => true. If you don't want to save your schema files then make it false.
Schema folder can be configured in config. set 'path' => 'schemafiles' => 'folderpath'.
Pass --save
option to command to save file,
php artisan infyom:scaffold $MODEL_NAME --save
Repository pattern
If you don't want repository pattern then you can just disabled it from config.
It's enabled by default, in the configuration file in 'options' => 'repository_pattern' => true. If you don't want to generate repository then make it false.
If you have not publish BaseRepository yet than must be publish BaseRepository first using the following command php artisan infyom:publish
to use repository patten.
Generate Factory
The Generator also provides an option to generate Factory class along with all of the model fields with Faker integration which can be directory used in Tests.
Pass --factory
option to command to generate Factory file,
php artisan infyom:scaffold $MODEL_NAME --factory
Generate Seeder
The provides an option to generate Seeder class for the occasions where you need to seed some data into database.
Pass --seeder
option to command to generate Seeder file,
php artisan infyom:scaffold $MODEL_NAME --seeder
Fields From File
If you have schema files stored then it can be used with generator rather than entering schema from the console.
You can find a sample file at vendor\infyom\laravel-generator\samples\fields_sample.json
.
To use schema file, use --fieldsFile
option.
php artisan infyom:scaffold $MODEL_NAME --fieldsFile=filename_from_model_schema_directory_OR_path_from_base_directory_OR_absolute_file_path
Here is the definition and possible options for schema field:
{
"name": "title",
"dbType": "string,50",
"htmlType": "text",
"validations": "required",
"searchable": true,
"fillable": true,
"primary": false,
"inForm": true,
"inIndex": true
}
You can also define relationship in field as following:
{
"name": "writer_id",
"dbType": "integer:unsigned:foreign,writers,id",
"htmlType": "text",
"relation": "mt1,Writer,writer_id,id"
}
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:
{
"type": "relation",
"relation": "1tm,Comment,post_id"
}
Custom Table Name
You can also specify your own custom table name by,
php artisan infyom:scaffold $MODEL_NAME --tableName=custom_table_name
Generate From Table
php artisan infyom:scaffold $MODEL_NAME --fromTable --tableName=$TABLE_NAME
Generate from specific connection (database)
When you have more than one connections (databases), for e.g, database1
and database2
and you want to create scaffold, api or api_scaffold from specific connection's table at that time you can use this option.
php artisan infyom:scaffold $MODEL_NAME --fromTable --tableName=$TABLE_NAME --connection=connectionName
Skip File Generation
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.
php artisan infyom:api_scaffold Post --skip=routes,migration,model
You can specify any file from the following list:
- migration
- model
- controllers
- api_controller
- scaffold_controller
- scaffold_requests
- routes
- api_routes
- scaffold_routes
- views
- tests
- menu
- dump-autoload
Custom Primary Key Name
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.
php artisan infyom:scaffold $MODEL_NAME --primary=custom_name_id
Custom Plural Name
If you have a model name where you need to pass it's plural name manually which sometimes makes more sense then you can do this with the following option. Sometimes it makes sense while using it with different languages.
php artisan infyom:scaffold $MODEL_NAME --plural=AuthorBooks
Prefix 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.
If you want to add multiple prefix than prefix separate by slash . Like, v1/admin
then you can use --prefix=v1/admin
option.
php artisan infyom:scaffold $MODEL_NAME --prefix=admin
Ignore Fields
While generating from table, if you want to skip certain type of fields like GeoPoint, Last Login time etc. which you do not expect user to insert via CRUD form, then you can use this option to specify those ignored fields.
php artisan infyom:scaffold $MODEL_NAME --ignoreFields=geo_location,last_login
Force Migration
If you want to run migration at the end of the CRUD generation without the console prompt, the you can use this option.
php artisan infyom:scaffold $MODEL_NAME --forceMigrate
API Resource Generator
If you want to use Laravel Eloquent Resources in your API, then you can use this option.
All you have to do is change the value into generator's config file. change the options.resources
=> true
or you can use following command to generate it for specific API.
php artisan infyom:api $MODEL_NAME --resources=true