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.2 can be found here.
This demo was generated using the following two commands
php artisan resource-file:create Biography --fields=name,age,biography,sport,gender,colors,is_retired,photo,range,month php artisan create:resources Biography --with-form-request --models-per-page=15 --with-migration
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) (You may 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 design your own templates using a different or no css framework. For more info on how to create a custom template click here!
Below a video to demonstrate how to use most of the package commands.
The option in between the square brackets []
must be replaced with a variable of your choice.
The following example assumes that we are trying to create a CRUD called AssetCategory with the fields listed below.
php artisan resource-file:create AssetCategory --fields=id,name,description,is_active
The above command will create resource-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 resource-file:create AssetCategory --fields=id,name,description,is_active --translation-for=en,ar
The above command will create resource-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 resource-file:create AssetCategory --fields=id,name,description,is_active
The above command will create resource-file names /resources/codegenerator-files/asset_categories.json
php artisan create:resources AssetCategory --with-form-request
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 resource-file:create AssetCategory --fields=id,name,description,is_active
The above command will create resource-file names /resources/codegenerator-files/asset_categories.json
php artisan create:resources AssetCategory --with-soft-delete --with-migration
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
The above command will create resource-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 resource-file from existing database separately using
php artisan resource-file:form-database AssetCategory
php artisan create:resources AssetCategory --table-exists --translation-for=en,ar
The above command will create resource-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 resource-file from existing database separately using
php artisan resource-file:form-database AssetCategory --translation-for=en,ar
php artisan resource-file:form-database AssetCategory --translation-for=en,ar
php artisan create:resources AssetCategory
The above command will create resource-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!
Laravel-Code-Generator strive to generate highly readable, and error free code. In order to keep your code readable, it is important to follow a good naming convention when chosing names for your models, fields, tables, relations and so on. Here is a list of recommendation that we belive is important to keep your code clean and highly readable.
Post
and PostCategory
...users
, post_categories
...id
in the table._id
. For example, post_id
, user_id
, post_category_id
...title
, first_name
, description
...Version 2.2 introduces very exciting features, more flexibility and less work for you out of the box! It also, adds support for the new features that were introduced in Laravel 5.5. Follow is a list of all new features and changes that were introduced.
Smart Migrations Engine
Whaaaat?!! Yup that's right, version 2.2 introduce a very powerful feature which keeps track of all your migrations. After migrating, each time, you add/delete a field/index from your resource file, the code-generator will only generate a migration to add/drop and drop/add columns as needed! Keep in mind that you still have to tell the generator that you need to create a new migration using
create:migration
command or the--with-migration
option for thecreate:resources
command.Another migration related feature was to organizing your migration files! When uses migrations heavily, finding a specific migration may be overwhelming due to the number of file. This feature, allow you to group all your migrations into sub-folders. Please note that this feature is off by default, to turn it on, set organize_migrations to true.
You're probably thinking "Laravel only detects migrations in the main folder... boooo!" That is correct! However, if you are using Laravel 5.3+, version 2.2 of Laravel-code-generator include five new commands to help you interact with migration from all folders. Check out the "Command Changes" below for more info about the new commands.
Previously Laravel-Code-Generator was limited to
belongsTo()
type relation. Now, when creating resources from existing database's table, the code-generator is able to createhasOne()
andhasMany()
relations by scanning the database's constrains and analyzing its existing data.In the resource-file you can now define any Eloquent relations. Each relation should follow the foreign-relation schema below. Additionally, you can define composite/multi-columns indexes! Each index should follow the index schema listed below.
When using Laravel 5.5, you can pass custom Validation Rule object directly in you resource file and the generator will add it to the validation rules! For more info check out the validation option below
Improved the file uploading process to allow you to delete uploaded file
--indexes
and--relations
have been added to the following commandsresource-file:create
,resource-file:append
, orresource-file:reduce
to allow you to interact with the resource-file freely.The options
--fields
,--indexes
and--relations
for theresource-file:create
,resource-file:append
, orresource-file:reduce
commands accept complex string to allow you to pass more values to add to the resource-file. For example,--fields="name:colors;html-type:select;options:blue|yellow|green|red|white,name:second_field_name"
plural_names_for was added to the configuration file to allow you to set your own plural-form vs singular-form preference when naming controller, form-request, resource-file, language file, table-name and route group. If you like your controllers to be in a plural-form, you can simply change the default behavior from the configuration file!
controller_name_postfix was added to the configuration file to allow you to change the controller's postfix. If you don't like to post fix your controllers with the word Controller, you can set this to an empty string or any other value.
form_request_name_postfix was added to the configuration file to allow you to change the form-request's postfix. If you don't like to post fix your form-request with the word FormRequest, you can set this to an empty string or any other value.
irregular_plurals was added to the configuration file. The code-generator heavily uses Laravel helpers
str_plural()
andstr_singular()
to generate readable code to make your code spectacular. The problem is the both generate incorrect words for irregular plurals. If you are using a language other than English, you can define a word with each with its plural-form to help the generator keep your code readable.create_move_file_method was added to the configuration file. This option will allow the user to chose not to create moveFile method on every CRUD when file-upload is required. If you set this to false, it is your responsibility make sure that the moveFile method exists in a higher level of your code like
App\Http\Controllers\Controller
.New configuration file (i.e
config/code_generator_custom.php
) was added to allow you to override the default configuration. This way, you won't lose any of your custom configuration when upgrating which is important! For more info, read the config file.
In addition to storing fields in the JSON file, indexes and relations can be stored in the same file too! For that reason, the option --fields-file have been renamed to --resource-file in all the commands.
Version 2.2 completly dropped support for raw fields, indexes, and relations as announced in previous documents. Storing resources in JSON file is much better, easier to manage, easier to regenerate resources in the future, shorter/cleaner commands, and much more flexible!
Thanks to the request validation improvment in Laravel 5.5, the controller code is much cleaner.
When the
ConvertEmptyStringsToNull
middleware is registered, we no longer convert empty string to null manually since the middleware will do just that.The
--without-migration
option withphp artisan create:resources
command has been reversed. It is now--with-migration
and should only be passed when you need a new migration created.For consistency, the --lang-file-name option have been renamed to --language-filename.
The options
--names
in theresource-file:create
,resource-file:append
, andresource-file:reduce
has been renamed to--fields
.
The following commands were renamed
The command
create:fields-file
has been renamed toresource-file:from-database
The command
fields-file:create
has been renamed toresource-file:create
The command
fields-file:delete
has been renamed toresource-file:delete
The command
fields-file:append
has been renamed toresource-file:append
The command
fields-file:reduce
has been renamed toresource-file:reduce
The following commands were added
php artisan migrate-all
command was adde. It allow you to run all of your outstanding migrations from all folders
php artisan migrate:rollback-all
command was added and it allows you to rolls back the last "batch" of migrations, which may include multiple migration from all folders.
php artisan migrate:reset-all
command was added to allow you to roll back all of your application's migrations from all folder.
php artisan migrate:refresh-all
command was added to allow you to invoke themigrate:rollback-all
command then immediately invokes themigrate:migrate-all
command.
php artisan migrate:status-all
command was added to allow you to checks the status of all your migration from all folders.
All known bugs have been addressed!
crestapps/laravel-code-generator
dependency to 2.2.*
.composer update
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default --force
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default-collective --force
resouces/codegenerator-templates
to resources/laravel-code-generator/templates
. IMPORTANT do not copy the default and default-collective folders.resouces/codegenerator-files
to resources/laravel-code-generator/sources
. Now you should be able to delete the following two folders since they have been relocated resouces/codegenerator-templates
and resouces/codegenerator-files
.php artisan create:layout "My New App"
. If you are using your own layout, you may want to create a temporary layout and extract the updated css/js code into your own layout/assets. The following command will create a new file called "app_temp.blade.php" php artisan create:layout "My New App" --layout-filename=app_temp
php artisan resource-file:create Post --fields=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. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--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 |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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 --table-name option. When using this option, the command This option is currently available only for MySql database only. 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 but it will get everything setup for you. If this option is left out, no translation key's will be generated. For example, passing
This option will only work when using --table-exists option otherwise it is ignored.
|
--language-filename |
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 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 |
--with-migration |
This option will create a migration for your resource.
Behind the scenes, this option invokes the
create:migration command to create the required migration.
|
--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. |
|
|
--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 --table-name option. When using this option, the command This option is currently available only for MySql database only. 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 but it will get everything setup for you. If this option is left out, no translation key's will be generated. For example, passing
This option will only work when using --table-exists option otherwise it is ignored.
|
--language-filename |
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 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 |
--with-migration |
This option will create a migration for your resource.
Behind the scenes, this option invokes the
create:migration command to create the required migration.
|
--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.json 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". |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--routes-prefix |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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. |
--language-filename |
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 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 |
--without-languages |
Allow you to create all the resources excluding the language file if one is needed. Note: the language file will only be created if the resource file contains translations. |
--without-model |
Allow you to create all the resources excluding the model. |
--without-controller |
Allow you to create all the resources excluding the controller. |
--without-form-request |
Allow you to create all the resources excluding the form-request if one is used. Note: when creating a controller with a form-request the form-request is injected into the action methods. Thus, in order to create the form-request based controller, you would have to use |
--without-views |
Allow you to create all the resources excluding the 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. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--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". |
--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 |
--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 |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--routes-prefix |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--routes-prefix |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--routes-prefix |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--routes-prefix |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--routes-prefix |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--routes-prefix |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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". |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--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. |
--engine-name |
A specific engine name for the database's table can be provided here. |
--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 migration if one already exists. |
|
|
model-name |
The name of the model. |
--class-name |
The name of the form-request class. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--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 |
Default: default-form Prefix of the route group. For example, if the word "frontend" was provided, it assumes that all the generated routes start with /frontend/. By default, the route-prefix is the plural-form of the model name. However, this is something can be changed from the configuration file |
--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-filename |
The language file name to store the keys under. Typically, this would be the plural-form of the model name. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--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 views if any already exists. |
|
|
model-name |
The name of the model that these fields will represent. |
--resource-filename |
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. |
--fields |
A list of the field names to be created. The names should be separated by a comma. You may also pass a complex string using the following schema
Complex string are allowed and will be handy is some cases. However, in most cases all you need to pass is the field names as the Each field in the complex string must be seperated by a |
--relations |
A list of the relations to be created. The string should follow the schema below
Each relation in the string must be seperated by a |
--indexes |
A list of the indexes to be created. The string should follow the schema below
Each index in the string must be seperated by a |
--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 but it will get everything setup for you. 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. |
--resource-filename |
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. |
--fields |
A list of the field names to be added. The names should be separated by a comma. You may also pass a complex string using the following schema
Complex string are allowed and will be handy is some cases. However, in most cases all you need to pass is the field names as the Each field in the complex string must be seperated by a |
--relations |
A list of the relations to be added. The string should follow the schema below
Each relation in the string must be seperated by a |
--indexes |
A list of the indexes to be added. The string should follow the schema below
Each index in the string must be seperated by a |
--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 but it will get everything setup for you. If this option is left out, no translation key's will be generated. For example, |
|
|
model-name |
The name of the model that these fields will represent. |
--resource-filename |
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. |
--fields |
A list of the field names to be created. The names should be separated by a comma. |
--relations |
A comma seperated relation names to remove from the resource file. |
--indexes |
A comma seperated index names to remove from the resource file. |
|
|
model-name |
The name of the model that these fields represents. |
--resource-filename |
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 resource's 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. |
--resource-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 resource from. This option allows you to have all resources such as fields, indexes and relations 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 resource-file can be found in the "Managing fields using JSON file" section. |
--resource-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 but it will get everything setup for you. 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. |
One of the powerful feature of this package is that is allows you to create your own custom templates! To create a custom template, simply duplicate /resources/codegenerator-templates/default folder, then name it anything you like. For example, bluesky.
Now, make all the custom changes in the bluesky folder.
To use the bluesky templete, you can pass it when needed for each command using --template-name=bluesky
option. Or, if you like to make it the default template for all of you commands, open the config file config/codegenerator.php file, change the value of the template
key to bluesky.
Although you can change just about anything in the template, you may only want to change the views related files that ends with .blade.stub
If you like to create a custom template with Laravel-Collective click here for more info.
A JSON based file that allows you to define how you like your resource generated. You can define your fields, indexed, and model relations.
The minimum requirement for creating a field is a unique name. However, the code-generator is very flexible and allows you to have full control on the fields. Below all the available properties for defining a field
General Properties | |
name |
Required A unique name for the field. |
label |
A user-friendly title to describe the field. If this option is left out, the field's name is used to generate a title. |
validation |
You can pass any valid Laravel validation rule. The rules should be separated by bar For example: Start with Laravel 5.5, you can define custom validation rules and pass them as well. For example, to use a custom validation rule called Uppercase in addition to the 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 Eloquent-mutator 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 appended 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 #Example 1, a simple arrayIn this option, the value will be the numeric index value of the item in the array. "options": [ "Prefer not to say", "Male", "Female" ] #Example 2, explicit values"options": { "": Prefer not to say", "male": "Male", "female": "Female" } #Example 3, multiple language phrases for each option"options": { "en": { "": "Prefer not to say", "male": "Male", "female": "Female" }, "ar": { "": "Prefer not to say in Arabic", "male": "Male in Arabic", "female": "Female in Arabic" }, "fr": { "": "Prefer not to say in French", "male": "Male in French", "female": "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 resource-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 resource-file:create, resource-file:from-database or resource-file:delete commands, a file called resources_map.json
is automaticly updated.
The following is the structure of the file.
{ { "model-name": "Brand", "resource-file": "brands.json" }, { "model-name": "Customer", "resource-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
{ { "model-name": "Customer", "resource-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.
If you're using a code-first-approach and like to define relations between your models, you can easily define that in the relations
keyword entry of the resource-file. Each relation can be defined using the following schema
{ "name": "posts", // the name of the relation "type": "hasMany", // the type of the relation "params": [ // the parameters for the used relation. "App\\Models\\Comment", "post_id", "id" ], "field": "name" // the name of the field on the foreign model to use as display value }
When creating hasOne()
or belongsTo() relations, it be best to define them at the field level using the foreign-relation option.
If you're using a code-first-approach and like to define indexes with multiple columns, you can easily do that by adding these indexed to the Indexes keyword entry in the resource-file file. Each composite index can be defined using the following schema
{ "name": "owner", // The name of the index to use, if no name is set a one will be generated. "type": "unique", // Valid index type is one of the following 'index','unique' or 'primary'. If the type is not provided, 'index' is used. "columns": [ // List of the columns' names to be included in the index left to right. "first_name", "last_name" ] }
Laravel-Code-Generator ships with lots of configurable option to give you control of the generated code. It is strongly recommended that you read the comments block above each option in the config/codegenerator.php file to get familer with all available options.
Starting at version 2.2 it ships with a unique way to override/extend the default settings to prevent you from losing your setting when upgrading the package. The config/codegenerator_custom.php is a dedecated file to store your options. This file will always be controlled by you and will never be overriden by the package. To override any configuration found in config/codegenerator.php, simple add the same option in your custom file. The generator will look at the your configuration before falling back to the default config. Note, any array based option will be extended not overriden. For more info read the comment block in the config/codegenerator_custom.php
The most important option in the configuration file is common_definitions. This option allows you to set the default properties of new field using the name of that field. Your goal should be to generate 100% ready resource-file using this config. It will save you lots of time since all your fields will get generated using the desired properties. In another words, when using resource-file:create
, resource-file:append
or resource-file:from-database
to create resource file, the generated JSON will be 100% ready for you without any manual modification.
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
todefault-collective
Or, 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.