A clean code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages or request-forms is seconds! It is extremely flexible and customizable to cover many use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application. The source code of this project can be found at GitHub and available under MIT license
A live demo of a site which was generated using laravel code generator v2.1 can be found here.
This demo was generated using the following two commands
php artisan fields-file:create Biography --names=name,age,biography,sport,gender,colors,is_retired,photo,range,month php artisan create:resources Biography --with-form-request --models-per-page=15
This demonstration allows you to list, add, update or delete records. However, it will not commit anything to the database.
Laravel-Code-Generator is fully capable of generating views using Laravel-Collective. To Generate views using it, you must first install Laravel-Collective into your project. Instructions on how to use it can be found below
1) To download this package into your laravel project, use the command-line to execute the following command
composer require crestapps/laravel-code-generator --dev
2) (Skip this step when using Laravel >= 5.5) To bootstrap the packages into your project while using command-line only, open the app/Providers/AppServiceProvider.php file in your project. Then, add the following code to the register()
method.
if ($this->app->runningInConsole()) { $this->app->register('CrestApps\CodeGenerator\CodeGeneratorServiceProvider'); }
3) Execute the following command from the command-line to publish the package's config and the default template to start generating awesome code.
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default
A layout is required for the default views! The code generator allows you to create a layout using the command-line. Of course you can use your own layout. You'll only need to include CSS bootstrap framework in your layout for the default templates to work properly. Additionally, you can chose to you design your own templates using a different or no css framework.
The option in between the square brackets [] must be replaced with a variable of your choice.
Lets create a CRUD called AssetCategory with the fields listed below.
php artisan fields-file:create AssetCategory --names=id,name,description,is_active
The above command will create fields-file names /resources/codegenerator-files/asset_categories.json
php artisan create:resources AssetCategory
The above command will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!
php artisan fields-file:create AssetCategory --names=id,name,description,is_active --translation-for=en,ar
The above command will create fields-file names /resources/codegenerator-files/asset_categories.json
php artisan create:resources AssetCategory
The above command will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!
php artisan create:resources AssetCategory --table-exists --translation-for=en,ar
The above command will create fields-file names /resources/codegenerator-files/asset_categories.json
Then it will create a model app/Models/AssetCategory, a controller app/Http/Controllers/AsseyCategoriesController, all views and the routes!
You may also create a fields-file from existing database separately using
create:fields-file AssetCategory --translation-for=en,ar
Version 2.1 introduces more flexibilty, less work for you out of the box! Follow is a list of all new features and changes that were introduced.
When generation a resources for a multiple-languages based application, Laravel-Code-Generator provides you with a translation to everything out of the box! That means in addition to the fields, all button, labels, placeholder and title will have a locale key.
generic_view_labels was added to the configuration file to allow you to add translations for the generic labels like button and title with in the view. You can even add custom templates to automaticly replace!
placeholder_by_html_type was added to the configuration file to allow you to generate placeholder text by html-type when no placeholder is provided. This will save you from having to manually create placeholder text for your fields. If you don't like to use placeholder for text, you can easily remove `'text' => 'Enter [% field_name %] here...',` from the configuration.
Yes! You are now able to create multiple resources using a mapping file. In other words, you can create complete resources for your entire database using one command!
For the sake of consistency and to make the commands easier to use and remember, almost all commands now accept the same argument which is the model name. (ex, Asset, AssetCategory...)
You no longer need to add the fields file name as an option (ex.
--fields-file:assets.json
), it is automaticly passed. For example, when using php create:resources AssetCategory the generator will look for a file called assets_categories.json.
php artisan fields-file:delete allows you to delete existing fields file. Using this command also updates the resources mapping file.
php artisan create:mapped-resourced allows you to generate multiple resources all at once.
All known bugs have been addressed!
When using Laravel >= 5.5, there is no need to update the config/app.php file to bootstrap the package! Laravel will take care of that for you using the composer.json file.
crestapps/laravel-code-generator
dependency to 2.1.*
in your composer.json file.composer update
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default --force
CrestApps\CodeGenerator\CodeGeneratorServiceProvider::class,
line from the providers array.register()
method.
if ($this->app->runningInConsole()) { $this->app->register('CrestApps\CodeGenerator\CodeGeneratorServiceProvider'); }
php artisan fields-file:create Post --name=id,title,details,is_active
|
|
application-name |
The name of your application |
--layout-filename |
Default: app The name of the layout file to be used. In this example the layout file will be "app.blade.php" |
--layout-directory |
Default: layouts The directory to create the layout under. |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--without-validation |
This option allows you to create a layout without any client-side validation Note: client-side validation will boost your app performance. |
--force |
This option will override the layout if one already exists. |
|
|
model-name |
The model name to create resources for. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--controller-name |
The name of the controller to create. If the provided value does not end with the word "Controller" it will be appended. If this option is left out, the controller's name will be generated using the plural-form of the giving model's name. In the above example, the controller will be called "PostsController". |
--controller-extends |
Default: Here you can specify which class should the controller extends. By default, we extend You may pass the value |
--with-auth |
When used, this option will add the In order to use this option, you must enable Laravel's built in authentication. |
--routes-prefix |
Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. |
--models-per-page |
Default = 25 The amount of models to show per page on the index view. By default, the "Index" method will display 25 records per page. |
--with-form-request |
Instead of placing the field's validation rules directly in the controller class, this option will extract the rules into a separate form-request class. The form-request class allows you to do more complex validation, cleans up your controller, and increases your code reusability. By default, the method When using --with-auth option, the |
--table-name |
The database's table name. If this option is left out, it is assumed that the table name is the plural-form of the model-name. In the above example, the table name will be "posts". If the model name is AssetCategory, the table name will be "asset_categories". |
--table-exists |
This option allows you to generate resources from existing database table. When this option is used, the database's name is assumes to be the plural-form of the provided "model-name". Of course, the table name can be set to a different value by passing the When using this option, a fields' file will be automatically generated. The generated file name will be names exactly like the database's table name. This file will allow you to change the default behavior and recreate the view to fit your needs. This option is currently available only for MySql database. It will not work if used with a different driver. Note: To create multiple-language translation from existing database, use the --translation-for option. |
--translation-for |
A comma separated languages. When creating resources from existing database using the --table-exists options, --transation-for allows you to create multi-language labels. You still have to provide translation for the corresponding language. If this option is left out, no translation key's will be generated. For example,
This option will only work when using --table-exists option.
|
--lang-file-name |
The languages file name to put the labels "if any" in. If one isn't provided, the file name will be the plural-form of the provided model name. Note: if the file already exists, and the same key "field name" exists in the file, no code message will be added.
This option will only work when using --table-exists option.
|
--fillable |
A comma separated string to put in the fillable property of the model. For example You may also set the fillable fields from the fields property directly. When this option is left out, any field that is flagged
This option will be deprecated in future releases. Instead, set the property
is-on-form to true in the fields-file.
|
--relationships |
A string to define the relationships to other models. This option allows you to create relationship between different models. For example, to build a has-many relation between "Post" model and "Comment" model the following string is used "comments#hasMany#App\Models\Comment|id|post_id" Here is a description of the string read from left to right using the comments the relationship name "aka the method name" hasMany the type of relation. App\Models\Comment first parameter to pass to Id second parameter to pass to post_id third parameter to pass to For more info about the available relations and the required parameters, visit Laravel documentation
This option will be deprecated in future releases. Instead, use the property
foreign-relation in the fields-file to set the relation.
|
--primary-key |
Default = id The field's name of the primary key. The default value can be overridden by setting the is-auto-increment or the is-primary flag to true in the fields setup. |
--with-soft-delete |
Enables the soft-delete feature that eloquent provides. |
--without-timestamps |
Prevent Eloquent from maintaining both |
--indexes |
A list of index can be provide here. You may wish to create composite index. Additionally, you can set the single field to be an indexes from the fields setup. This can be done by setting the is-index, is-unique, or is-primary properties of a field. Here is an example, |
--foreign-keys |
You can provide a list of foreign keys to add. Here is an example, This example will generate the following code $table->foreign('user_id') ->references('id') ->on('users') ->onDelete('cascade') ->onUpdate ('cascade'); $table->foreign('deleted_by') ->references('id') ->on('users');
This option will be deprecated in future releases. Instead, use the property
foreign-constraint in the fields-file to set the relation.
|
--without-migration |
When using a database first approach, migrations are not necessary. Providing this option will prevent the resources command from creating a migration file. |
--migration-class-name |
The name of the migration class. If this option is not set, a name will be generated based on the model name. |
--connection-name |
Eloquent uses the configured default database connection. Providing a value here will tell Eloquent to connect using the provided connection. |
--engine-name |
A specific engine name for the database's table can be provided here. |
--controller-directory |
The directory where the controller should be created under. For example, if the word "Frontend" was provided, the controller will be created in App/Http/Controllers/Frontend directory. The default path where the controller will be created can be set from the config file |
--model-directory |
A directory where the model will be created under. The default path where the model will be created can be set from the config file |
--views-directory |
The name of the directory to create the views under. If this option is left out, the views will be created in /resources/views |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--force |
This option will override any existing files if any exists. |
When using fields-file:create, create:fields-file and fields-file:delete updates the resources_map.json
file. This option can be used to generate resources for all the mapped resources.
|
|
--controller-extends |
Default: Here you can specify which class should the controller extends. by default, we extend You may pass the value |
--with-auth |
When used, this option will add the In order to use this option, you must enable Laravel's built in authentication. |
--models-per-page |
Default = 25 The amount of models to show per page on the index view. By default, the "Index" method will display 25 records per page. |
--with-form-request |
Instead of placing the field's validation rules directly in the controller class, this option will extract the rules into a separate form-request class. The form-request class allows you to do more complex validation, cleans up your controller, and increases your code reusability. By default, the method When using --with-auth option, the |
--form-request-directory |
The directory where the form-request should be created under. For example, if the word "Frontend" was provided, the form-request will be created in App/Http/Requests/Frontend directory. The default path where the form-request will be created can be set from the config file |
--table-exists |
This option allows you to generate resources from existing database table. When this option is used, the database's name is assumes to be the plural-form of the provided "model-name". Of course, the table name can be set to a different value by passing the When using this option, a fields' file will be automatically generated. The generated file name will be names exactly like the database's table name. This file will allow you to change the default behavior and recreate the view to fit your needs. This option is currently available only for MySql database. It will not work if used with a different driver. Note: To create multiple-language translation from existing database, use the --translation-for option. |
--translation-for |
A comma separated languages. When creating resources from existing database using the --table-exists options, --transation-for allows you to create multi-language labels. You still have to provide translation for the corresponding language. If this option is left out, no translation key's will be generated. For example,
This option will only work when using --table-exists option.
|
--lang-file-name |
The languages file name to put the labels "if any" in. If one isn't provided, the file name will be the plural-form of the provided model name. Note: if the file already exists, and the same key "field name" exists in the file, no code message will be added.
This option will only work when using --table-exists option.
|
--primary-key |
Default = id The field's name of the primary key. The default value can be overridden by setting the is-auto-increment or the is-primary flag to true in the fields setup. |
--with-soft-delete |
Enables the soft-delete feature that eloquent provides. |
--without-timestamps |
Prevent Eloquent from maintaining both |
--without-migration |
When using a database first approach, migrations are not necessary. Providing this option will prevent the resources command from creating a migration file. |
--connection-name |
Eloquent uses the configured default database connection. Providing a value here will tell Eloquent to connect using the provided connection. |
--engine-name |
A specific engine name for the database's table can be provided here. |
--controller-directory |
The directory where the controller should be created under. For example, if the word "Frontend" was provided, the controller will be created in App/Http/Controllers/Frontend directory. The default path where the controller will be created can be set from the config file |
--model-directory |
A directory where the model will be created under. The default path where the model will be created can be set from the config file |
--views-directory |
The name of the directory to create the views under. If this option is left out, the views will be created in /resources/views |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--force |
This option will override existing files if any exists. |
--mapping-filename |
This option allows you to pass the name of the mapping file. When this option is left out, the default resources_map file will be used. |
|
|
model-name |
The model name to create the controller for. |
--controller-name |
The name of the controller to create. If the provided value does not end with the word "Controller" it will be appended. If this option is left out, the controller name is generated from the provided model name. In the example above, the controller name will be "PostsController". |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--routes-prefix |
Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. |
--models-per-page |
Default = 25 The amount of models to show per page on the index view. By default, the "Index" method will display 25 records per page. |
--lang-file-name |
The languages file name to put the labels "if any" in. If one isn't provided, the file name will be the plural-form of the provided model name. Note: if the file already exists, and the same key "field name" exists in the file, no code message will be added. |
--controller-extends |
Default: Here you can specify which class should the controller extends. by default, we extend You may pass the value |
--with-auth |
When used, this option will add the In order to use this option, you must enable Laravel's built in authentication. |
--with-form-request |
Instead of placing the field's validation rules directly in the controller class, this option will extract the rules into a separate form-request class. The form-request class allows you to do more complex validation, cleans up your controller, and increases your code reusability. By default, the method When using --with-auth option, the |
--form-request-directory |
The directory where the form-request should be created under. For example, if the word "Frontend" was provided, the form-request will be created in App/Http/Requests/Frontend directory. The default path where the form-request will be created can be set from the config file |
--controller-directory |
The directory where the controller should be created under. For example, if the word "Frontend" was provided, the controller will be created in App/Http/Controllers/Frontend directory. The default path where the controller will be created can be set from the config file |
--model-directory |
A directory where the model will be created under. The default path where the model will be created can be set from the config file |
--views-directory |
The name of the directory to create the views under. If this option is left out, the views will be created in /resources/views |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--force |
This option will override the controller if one already exists. |
|
|
model-name |
The name of the model. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--table-name |
The database's table name. If this option is left out, it is assumed that the table name is the plural-form of the model-name. In the above example, the table name will be "posts". If the model name is AssetCategory, the table name will be "asset_categories". |
--fillable |
A comma separated string to put in the fillable property of the model. For example You may also set the fillable fields from the fields property directly. When this option is left out, any field that is flagged
This option will be deprecated in future releases. Instead, set the property
is-on-form to true in the fields-file.
|
--model-directory |
A directory where the model will be created under. The default path where the model will be created can be set from the config file |
--relationships |
A string to define the relationships to other models. This option allows you to create relationship between different models. For example, to build a has-many relation between "Post" model and "Comment" model the following string is used "comments#hasMany#App\Models\Comment|id|post_id" Here is a description of the string read from left to right using the comments the relationship name "aka the method name" hasMany the type of relation. App\Models\Comment first parameter to pass to Id second parameter to pass to post_id third parameter to pass to For more info about the available relations and the required parameters, visit Laravel documentation
This option will be deprecated in future releases. Instead, use the property
foreign-relation in the fields-file to set the relation.
|
--primary-key |
Default = id The field's name of the primary key. The default value can be overridden by setting the is-auto-increment or the is-primary flag to true in the fields setup. |
--with-soft-delete |
Enables the soft-delete feature that eloquent provides. |
--without-timestamps |
Prevent Eloquent from maintaining both |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--force |
This option will override the model if one already exists. |
|
|
model-name |
The model name that the routes will represent. |
--controller-name |
The name of the controller that these routes point to. If the provided value does not end with the word "Controller" it will be appended. If this option is left out, the controller's name will be generated using the plural-form of the giving model's name. In the above example, the controller will be called "PostsController". |
--routes-prefix |
Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
When creating views using the create:views, create:create-view or create:update-view an additionally view called "form-view" is created. The "form-view" contains the form fields to prevent code duplication.
|
|
model-name |
The model name that the created views will represent. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--routes-prefix |
Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. |
--only-views |
Default = create,edit,index,show,form The only views to be created. A comma separated string with the name of the views to create. |
--layout-name |
Default = layouts.app A different layout could be used to generate the views. This can easily be done by providing a different layout name. For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--views-directory |
The name of the directory to create the views under. If this option is left out, the views will be created in /resources/views |
--force |
This option will override any views if any already exists. |
|
|
model-name |
The model name that this view will represent. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--routes-prefix |
Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. |
--layout-name |
Default = layouts.app A different layout could be used to generate the views. This can easily be done by providing a different layout name. For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--views-directory |
The name of the directory to create the views under. If this option is left out, the views will be created in /resources/views |
--force |
This option will override the view if one already exists. |
|
|
model-name |
The model name that this view will represent. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--routes-prefix |
Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. |
--layout-name |
Default = layouts.app A different layout could be used to generate the views. This can easily be done by providing a different layout name. For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--views-directory |
The name of the directory to create the views under. If this option is left out, the views will be created in /resources/views |
--force |
This option will override the view if one already exists. |
|
|
model-name |
The model name that this view will represent. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--routes-prefix |
Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. |
--layout-name |
Default = layouts.app A different layout could be used to generate the views. This can easily be done by providing a different layout name. For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--views-directory |
The name of the directory to create the views under. If this option is left out, the views will be created in /resources/views |
--force |
This option will override the view if one already exists. |
|
|
model-name |
The model name that this view will represent. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--routes-prefix |
Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. |
--layout-name |
Default = layouts.app A different layout could be used to generate the views. This can easily be done by providing a different layout name. For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--views-directory |
The name of the directory to create the views under. If this option is left out, the views will be created in /resources/views |
--force |
This option will override the view if one already exists. |
|
|
model-name |
The model name that this view will represent. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--routes-prefix |
Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. |
--layout-name |
Default = layouts.app A different layout could be used to generate the views. This can easily be done by providing a different layout name. For example, if the physical path to a different layout was /resources/views/layouts/template/newlayout.blade.php then its name would be layouts.template.newlayout |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--views-directory |
The name of the directory to create the views under. If this option is left out, the views will be created in /resources/views |
--force |
This option will override the view if one already exists. |
|
|
model-name |
The name of the model. |
--table-name |
The database's table name. If this option is left out, it is assumed that the table name is the plural-form of the model-name. In the above example, the table name will be "posts". If the model name is AssetCategory, the table name will be "asset_categories". |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--migration-class-name |
The name of the migration class. If this option is not set, a name will be generated based on the table name. |
--with-soft-delete |
Enables the soft-delete feature that eloquent provides. |
--connection-name |
Eloquent uses the configured default database connection. Providing a value here will tell Eloquent to connect using the provided connection. |
--indexes |
You can provide a list of indexes here. However, you can also set the single column indexes from the fields. This can be done by setting the is-index, is-unique, or is-primary properties of a field. Here is an example, |
--foreign-keys |
You can provide a list of foreign keys to add. Here is an example, This example will generate the following code $table->foreign('user_id') ->references('id') ->on('users') ->onDelete('cascade') ->onUpdate ('cascade'); $table->foreign('deleted_by') ->references('id') ->on('users');
This option will be deprecated in future releases. Instead, use the property
foreign-constraint in the fields-file to set the relation.
|
--engine-name |
A specific engine name for the database's table can be provided here. |
--without-timestamps |
Prevent Eloquent from maintaining both created_at and the updated_at properties. |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--force |
This option will override the migration if one already exists. |
|
|
model-name |
The name of the model. |
--class-name |
The name of the form-request class. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--with-auth |
When used, this option will add the In order to use this option, you must enable Laravel's built in authentication. |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
--form-request-directory |
The directory where the form-request should be created under. For example, if the word "Frontend" was provided, the form-request will be created in App/Http/Requests/Frontend directory. The default path where the form-request will be created can be set from the config file |
--force |
This option will override the form-request if one already exists. |
|
|
model-name |
The name of the model that these fields will represent. |
--language-file-name |
The language file name to store the keys under. Typically, this would be the plural-form of the model name. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--template-name |
This option allows you to use a different template at run time. If this option is left out, the default template is used. Note: the default template can be set from the config file i.e config/codegenerator.php by setting the |
|
|
model-name |
The name of the model that these fields will represent. |
--file-name |
The name of the file to be created. When this option is left out, the file will be the plural-form of the model name. If the model name is AssetCategory, the file name will be asset_categories.json. |
--names |
A list of the field names to be created. The names should be separated by a comma. |
--data-types |
A list of the data type to be created. The types should be separated by a comma. |
--html-types |
A list of the html type to be created. The types should be separated by a comma. |
--without-primary-key |
By default, this command will add primary key field called |
--translation-for |
A comma separated languages. When creating a multi-language application, this option allows for creating translation key in multiple languages. You still have to provide translation for the corresponding language. If this option is left out, no translation key's will be generated. For example, |
--force |
This option will override the field's file if one already exists. |
|
|
model-name |
The name of the model that these fields will represent. |
--file-name |
The name of the file to be created. When this option is left out, the file will be the plural-form of the model name. |
--names |
A list of the field names to be added. The names should be separated by a comma. |
--data-types |
A list of the data type to be created. The types should be separated by a comma. |
--html-types |
A list of the html type to be created. The types should be separated by a comma. |
--translation-for |
A comma separated languages. When creating a multi-language application, this option allows for creating translation key in multiple languages. You still have to provide translation for the corresponding language. If this option is left out, no translation key's will be generated. For example, |
When all fields are reduces, the file itself will be deleted.
|
|
model-name |
The name of the model that these fields will represent. |
--file-name |
The name of the file to be created. When this option is left out, the file will be the plural-form of the model name. |
--names |
A list of the field names to be removed. The names should be separated by a comma. |
It is recommended to use this command to delete file instead of manualy deleting it. This command will also delete the mapped relation in the resource_map file.
|
|
model-name |
The name of the model that these fields represents. |
--file-name |
The name of the file to be created. When this option is left out, the file will be the plural-form of the model name. |
Are you looking to convert existing application to Laravel framework? Or, looking to use database-first instead of code-first approach? No problem! This package allows you to create a fields' file from existing database.
You can easily take advantage of this feature by passing --table-exists
option to the create:resources
command to automatically generate all the resources from existing database's table.
|
|
model-name |
The name of the model that represents these fields. |
--table-name |
The name of your existing database's table name. When this option is left out, the table name is the plural-form of the model name. If the model name is AssetCategory, the table name is assumed to be asset_categories |
--database-name |
The database name to look under. If this option is left out, the database name in the database connection is used. |
--fields-file | Default: the plural-form of the model name. If the model name is AssetCategory, the name will then be asset_categories.json The name of the file to import fields from. This option allows you have all fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy to configure the fields, then reuse the same fields in multiple command now or in the future. More documentation on how to manage fields-file can be found in the "Managing fields using JSON file" section. |
--fields |
Fields to use for creating the validation rules. More documentation about this option can be found in the "assign model fields from JSON file" section below. This option will be deprecated in future releases. It is recommended to use the --fields-file option instead. |
--fields-filename |
The name of the file to generate the fields in. If this option is left out, the generate name file will be the same name as the database's table. |
--translation-for |
A comma separated languages. When creating a multi-language application, this option allows for creating translation key in multiple languages. You still have to provide translation for the corresponding language. If this option is left out, no translation key's will be generated. For example, |
--force |
This option will override the field's file if one already exists. |
The minimum requirement for creating a field is a unique name. However, code generator is very flexible and allows you to have full control on the fields. Below all the available properties for a field
General Properties | |
name |
Required A unique name for the field |
label |
A title to describe the field. If this option is left out, the field name is used to generate an English title. |
validation |
You can pass any valid Laravel validation rule. The rules should be separated by bar |. For example: To learn more about the valid options please visit Laravel documentation When the rule required is not used, the field in the migration file will automatically become nullable. |
HTML Properties | |
html-type |
Default: text A valid property will be one of the following options
Note: when using file type, after the file is uploaded to the designated path, the filename is stored in the database by default. For everything to work properly, the data-type must be of some sort of a string type. Or modify the behavior of moveFile method to handle the new file. By default this process stores the uploaded file in the path defined in config file codegenerator.files_upload_path
Note: when using checkbox, or multipleSelect, the items are stored in the database as json string. Additionally, the items in the index or form views are displayed separated by the value provided in the |
delimiter |
Default: "; " When generating a form with checkbox or a select menu that accepts multiple answers, we need either store the results in a foreign model or store the records in a string field. By default, the code generator will convert the multiple options that a user selected into a json string before the results are stored using a mutators method. When the data is presented on the show and/or index views, the options are displayed separated by the value of the |
css-class |
Default: "" You can add custom css class(es) to the html input. Any value is placed in this option will be added to the field's |
date-format |
Default: "m/d/Y H:i A" Any field with the type date, time or datetime can be formatted different when it is displayed. You can change the display format using this option. |
html-value |
Default: null A default value to set the field to. When using multiple options based html-type like checkbox, multipleSelect you can set this property to array of values to set multiple values by default. Ex, ["Red","Green"] |
options |
Default = empty string If you used `select` for the `html-type` property, this is where you provide the options. Here are example of how to use it options=Male|Femle Or if you want to have a different value that the title, you can pass the options like so options=1:Male|2:Female However, when using --fields-file option to import json, you can define multiple language phrases for each option "options": { "en": { "": "Prefer not to say", "1": "Male", "2": "Female" }, "ar": { "": "Prefer not to say in Arabic", "1": "Male in Arabic", "2": "Female in Arabic" }, "fr": { "": "Prefer not to say in French", "1": "Male in French", "2": "Female in French" } } |
is-inline-options |
Default = false If the html-type is set to radio or checkbox, setting this option to |
placeholder or place-holder |
Default = empty string You can set a placeholder value when html-type is set to text, number, email, textarea orselect. |
is-on-index |
Default = true
Setting the value to |
is-on-form |
Default = true
Setting the value to |
is-on-show |
Default = true
Setting the value to |
is-on-views |
Default = true
Setting the value to |
is-header |
Default = false Only one field can be set to a header. The header field will be use as the page header in the show view. The key |
Database Properties | |
data-type |
Default = varchar The database column type. The following are valid types.
Note: you can add short cuts if needed to in the codegenerator.php config file. You can add new mapping to the eloquent_type_to_method array. |
data-type-params |
This option allows you to specify parameters for the data type. Please ensure you provide valid parameters otherwise unexpected behavior will occur. For example, varchar and char will only need a maximum of one integer parameter where double, decimal and float require two integer parameters. Command line example with specifying a decimal precision and scale:
Command line example "data-type-params": [ 5, 2 ]
If this option left out while some sort of a string data-type was used along with a max validation rule, the max value is used to limit the length of the sting in the database when a migration is generated.
|
data-value |
Default = null The default value for the database column. |
is-auto-increment |
Default = false
Setting this value to |
is-primary |
Default = false You can set this field as the primary for retrieving records from the database. You can only set one column as the primary. If you set multiple fields are primary, the first one will be selected and the rest will be ignored. Note: if you set the is-auto-increment field, this option will automatically get set. Ths only time this can be used is to create a primary field you don't wish for the database to auto assign it. |
is-index |
Default = false
Setting this value to |
is-unique |
Default = false
Setting this value to |
is-nullable |
Default = false
Setting this value to Note: when setting this option to true, the default value will be set to NULL unless you pass a different value to data-value
When the validation rule contains "nullable", "required_if", "required_unless", "required_with", "required_with_all", "required_without", "required_without_all" or does NOT contains "required" rule, this flag will automatically gets set.
|
is-unsigned |
Default = false
Setting this value to |
comment |
This option will allow you to add meta description of the field in the database. |
is-data |
This option will allow you to casts a data filed to a Carbon object. |
cast-as |
This option will allow you to cast a field to php's native type. |
foreign-relation |
This option will allow you to create a foreign relation between the models. { "name": "creator", // the name of the relation "type": "belongsTo", // the type of the relation "params": [ // the parameters for the used relation. "App\\User", "created_by" ], "field": "name" // the name of the field on the foreign model to use as display value } |
foreign-constraint |
This option will allow you to create a foreign relation between the models. { "field": "user_id", // the field name. "references": "id", // the field to reference on the foreign model. "on": "users", // the foreign model begin referenced. "on-delete":"cascade", // the on-delete action. "on-update":"cascade", // the on-update actions. "references-model": "App\\Models\\User" // the namespace of the foreign model. } |
on-store |
This option allows you to set a fixed value on the store action. For example, |
on-update |
Similar to |
Storing the field's specification in a json file enables you to easily reuse the field with multiple commands. It also allows you to recreate the resources in the future if you decided to add/remove fields after the views have been crafted. The json files are typically stored in /resources/laravel-generator. If you don’t like where these files are kept, you can change that path from the config/laravelgenerator.php file.
The following command should be used to manage the fields-file to make this process easier.
The resources-map file, is a json file that is used to keep track of the fields-file and the model classes to allow you to create the resources all at once.
The default file name is resources_map.json
and can be changed from the configuration file.
When using fields-file:create, create:fields-file or fields-file:delete commands, a file called resources_map.json
is automaticly updated.
The following is the structure of the file.
{ "1": { "model-name": "Brand", "fields-file": "brands.json" }, "2": { "model-name": "Customer", "fields-file": "customers.json", "table-name": "customers_table" } }
All option that are available to the create:resources
can be used in the mapping file to make creating resources for all models customizable. Here is an example
{ "1": { "model-name": "Customer", "fields-file": "customers.json", "table-name": "customers_table", "routes-prefix" "customers_prefix" } }
To generate all the resources mapped in the resources_map.json
file, use the following command
php artisan create:mapped-resources [model-name]
When using the commands that generate fields, our goal is to generate fields configured and ready for use without having to make any change to the generated fields.
While it is not possible to cover 100% of the use cases, Laravel-code-generator is shipped with a powerful configuration option to allow you to add conditions to handle your own use case.
The key common_definitions
in the /config/codegenerator.php file allows you match field name using pattern then set the properties accordingly.
For example, you may want to add a global date, time, or datetime picker using javascript for any field where its name ends with _at.
You can do that by adding the following entry
[ 'match' => ['*_at'], 'set' => [ 'class' => 'datetime-picker', ] ],
The same thing can be done for any field that ends with _date or starts wih date_of
[ 'match' => ['*_date','date_of_*'], 'set' => [ 'class' => 'date-picker', ] ],
Of cource, you can set any of the field's option like html-type, data-type, data-type-params or foereign relation. You can set the configuration as fits your enviornmant, then you'll be able to create fields-file ready to generate resources with minimal work!
The conditions are applied to each field top to bottom, the configuration at the bottom of the array will take presence over the once on the top in case multiple conditions were matched.
The code generator allows you to assign fields from a raw string. The following is an example of raw string.
--fields="name=title;is-on-show;is-on-form;is-on-index=false;label=Some title without translations#name=title;labels=en:Some description in English|fn:Some description in French;options=Test|1:Male|2:Female"
Any property that starts with the keyword "is" expected to be a boolean value (true|false). The value of that property will optional as the default value will be set to `true`.
Note: The fields are separated by a hash-tag `#`. Each property that belongs to a field is separated by semi-colon `;` Here is example of a valid string.
As shown in the example above, adding translation to the labels can easily be done. You can add a colon : before the label, and add list of languages separated by a bar |. To control the translation more efficiently, it is recommended use --fields-file option instead to provide a json formatted string.
Laravel-Code-Generator is capable of fully generating views using Laravel-Collective package. In fact, it is shipped with a template based on laravel-collective called "default-collective".
By defaut, the template "default-collective" is not published to the resources folder as it is not needed out of the box. To publish it, use the command-line to execute the following command.
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default-collective
There are two ways to generate views using Laravel-Collective
Via the package configuration
Open the config file of the package /config/codegenerator.php change the value of the key
template
from default to default-collectiveOr, via command-line
Change the template name at run time. In another words, pass the following option
--template-name=default-collective
from command-line
First, duplicate the folder /resources/codegenerator-templates/default-collective and name it anything your like.
Second, open up the package config file and add the new template name to the laravel_collective_templates
array.