This documentation belongs to an older version of the Laravel-Code-Generator package.

Laravel Code Generator

Introduction

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! 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

Features

  • Create full resource using a single command with/without migration or from existing database.
  • Allows you to save the field in a json file and recreate resources when the business need change.
  • Create standard CRUD controllers with simple or form-request validation.
  • Create model with relations.
  • Create named routes with and without group.
  • Create standard CRUD views.
  • Very flexible and rich with configurable options.
  • (Beta) Client-side validation.
  • File uploading handling.
  • Auto multiple-response storing in the database.
  • Create form-request.
  • Customizable view’s templates to enable you to change the standard look and feel of your application.
  • Create view's layouts with and without client-side validation.
  • Change the template at run time to generate different views.

Dependencies

The following dependencies are most likely exists in your project. If you are starting a fresh Laravel project, you'll only need to add LaravelCollective Forms & HTML package to get started.

Prerequisite

Default template dependencies

Client-side validation dependencies


Getting Started

Installation

If you don't have LaravelCollective Forms & HTML package installed, it will be installed for you. However you'll still have to update your config/app.php to bootstrap the package to the framework.

To download this package into your laravel project, use the command-line to execute the following command

composer require crestapps/laravel-code-generator --dev

To bootstrap the packages into your project, open the config/app.php file in your project and follow the

First, look for the providers array.

Add the following line to bootstrap laravel-code-generator to the framework.

CrestApps\CodeGenerator\CodeGeneratorServiceProvider::class,

Add the following line to bootstrap "LaravelCollective Forms & HTML package" to the framework.

Collective\Html\HtmlServiceProvider::class,

Second, look for the aliases array. Then, add the following two lines to create an aliase for "LaravelCollective Forms & HTML package".

'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,

Finally, 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"

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 framework.

Available Commands

The option in between the square brackets [] must be replaced with a variable of your choice.

  • php artisan create:layout [application-name]
  • php artisan create:resource [model-name]
  • php artisan create:controller [controller-name]
  • php artisan create:model [model-name]
  • php artisan create:routes [controller-name]
  • php artisan create:views [model-name]
  • php artisan create:create-view [model-name]
  • php artisan create:edit-view [model-name]
  • php artisan create:index-view [model-name]
  • php artisan create:show-view [model-name]
  • php artisan create:form-view [model-name]
  • php artisan create:migration [table-name]
  • php artisan create:form-request [class-name]
  • php artisan create:language [language-file-name]
  • php artisan create:fields-file [table-name]

How To...

All examples below with the --fields-file option assumes you have a file called post.json located in resources/codegenerator-fields directory with the json string from the example below.

How to create "views-layout"?

Command
php artisan create:views-layout [application-name]
Or
php artisan create:layout [application-name]
Example
php artisan create:layout "My New Laravel App"
application-name

The name of your application

--layout-filename

Default: app

The name of the layout file.

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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--without-validation

This options allows you to create a layout without any client-validation

Note: clien-side validation will boost your app performance.

--force

This option will override the layout if one already exists.

How to create resources?

Command
php artisan create:resource [model-name]
Example
php artisan create:resource Post --fields-file=post.json
model-name

The model name to create resources for.

--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.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" 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 genrated 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/.

--models-per-page

Default = 25

The amount of models to show per page on 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.

--with-form-request

Instead of placing the field's validation rules directly in the controller class, this option will extract the rules into a seperate form-request class. FormRequest class to allows you to do more complex validation.

By default, the method authorize() returns false for your application's security. This method must be modified to return true value for the store and update requests to be allowed. Otherwise, the request will beForbidden

--table

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".

--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 option.

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 behavios 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.

--fillable

A comma separated string to put in the fillable property of the model. For example 'title','description'

You may also set the fillables fields from the fields property directly. When this option is left out, any field that is flagged is-on-form will automatically be added to the fillable list.

--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 # as a seperator

comments the relationship name "aka the method name"

hasMany the type of relation.

App\Models\Comment first parameter to pass to hasMay method.

Id second parameter to pass to hasMay method.

post_id third parameter to pass to hasMay method.

For more info about the available relations and the required parameters, visit Laravel documentation

--primary-key

Default = id

The field's name of the primary key. The default value can be overriden 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 created_at and the updated_at properties.

--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, --index='update_at','name'#unique='some_unique_column'#unique=col1_name,col2_name

--foreign-keys

You can provide a list of foreign keys to add.

Here is an example, --foreign-keys=user_id|id|users|cascade|cascade#deleted_by|id|users

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');
--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 table 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 config/codegenerator.php

--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 config/codegenerator.php

--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--force

This option will override existing files if any exists.

How to create a controller?

Command
php artisan create:controller [controller-name]
Example
php artisan create:controller Posts --fields-file=post.json
controller-name

The name of the controller to create. If the provided value does not end with the word "Controller" it will be appended.

--model-name

The model name to create resources for.

If this option is left out, the model name is generated from the provided controller name.

In the example above, the model name will be "Post".

--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.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--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 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.

--with-form-request

Instead of placing the field's validation rules directly in the controller class, this option will extract the rules into a seperate form-request class. FormRequest class to allows you to do more complex validation.

By default, the method authorize() returns false for your application's security. This method must be modified to return true value for the store and update requests to be allowed. Otherwise, the request will beForbidden

--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 config/codegenerator.php

--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 config/codegenerator.php

--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--force

This option will override the controller if one already exists.

How to create a model?

Command
php artisan create:model [model-name]
Example
php artisan create:model Post --fields-file=post.json
model-name

The name of the model.

--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.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--table

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".

--fillable

A comma separated string to put in the fillable property of the model. For example 'title','description'

You may also set the fillables fields from the fields property directly. When this option is left out, any field that is flagged is-on-form will automatically be added to the fillable list.

--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 config/codegenerator.php

--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 # as a seperator

comments the relationship name "aka the method name"

hasMany the type of relation.

App\Models\Comment first parameter to pass to hasMay method.

Id second parameter to pass to hasMay method.

post_id third parameter to pass to hasMay method.

For more info about the available relations and the required parameters, visit Laravel documentation

--primary-key

Default = id

The field's name of the primary key. The default value can be overriden 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 created_at and the updated_at properties.

--template-name

This options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--force

This option will override the model if one already exists.

How to create routes?

Command
php artisan create:routes [controller-name]
Example
php artisan create:routes posts
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 genrated using the plural-form of the giving model's name.

In the above example, the controller will be called "PostsController".

--model-name

The model name that this controller will represent. If you leave this option out, the model name will be constructed based on the controller name.

It will be the singular-form of the controller name. In the above example, the model name will be "Post".

--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

How to create all standard CRUD views (i.e. Create, Read, Update and Delete)?

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.

Command
php artisan create:views [model-name]
Example
php artisan create:views Post --fields-file=post.json
model-name

The model name that this view will represent.

--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.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--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.

How to create "create-view"?

Command
php artisan create:create-view [model-name]
Example
php artisan create:create-view Post --fields-file=post.json
model-name

The model name that this view will represent.

--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.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--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.

How to create "edit-view"?

Command
php artisan create:edit-view [model-name]
Example
php artisan create:edit-view Post --fields-file=post.json
model-name

The model name that this view will represent.

--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.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--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.

How to create "index-view"?

Command
php artisan create:index-view [model-name]
Example
php artisan create:index-view Post --fields-file=post.json
model-name

The model name that this view will represent.

--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.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--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.

How to create "show-view"?

Command
php artisan create:show-view [model-name]
Example
php artisan create:show-view Post --fields-file=post.json
model-name

The model name that this view will represent.

--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.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--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.

How to create "form-view"?

Command
php artisan create:form-view [model-name]
Example
php artisan create:form-view Post --fields-file=post.json
model-name

The model name that this view will represent.

--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.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--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.

How to create a database migration?

Command
php artisan create:migration [table-name]
Example
php artisan create:migration posts --fields-file=post.json
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".

--fields

Fields to use for creating the validation rules. More documentation about this feature can be found in the "assign model fields from JSON file" section below.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" 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.

--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, --index='update_at','name'#unique='some_unique_column'#unique=col1_name,col2_name

--foreign-keys

You can provide a list of foreign keys to add.

Here is an example, --foreign-keys=user_id|id|users|cascade|cascade#deleted_by|id|users

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');
--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 options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--force

This option will override the migration if one already exists.

How to create form-request?

Command
php artisan create:form-request [class-name]
Example
php artisan create:form-request PostFormRequest --fields-file=post.json
class-name

The name of the form-request class.

--fields

Fields to use for creating the validation rules. More documentation about this feature can be found in the "assign model fields from JSON file" section below.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--template-name

This options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

--force

This option will override the form-request if one already exists.

How to create a language file?

Command
php artisan create:language [language-file-name]
Example
php artisan create:language posts
language-file-name

The language file name to store the keys under. Typically, this would be the plural-form of the model name.

--fields

Fields to use for creating the validation rules. More documentation about this feature can be found in the "assign model fields from JSON file" section below.

--fields-file

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--template-name

This options allows you to use a different template at run time.

If this option is left out, the template that is set default is used.

Note: the default template can be set in the config file i.e config/codegenerator.php by setting the template key.

How to create a fields' file from existing database?

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:resouce command to automatically generate all the resources from existing database's table.

Command
php artisan create:fields-file [table-name]
Example
php artisan create:fields-file posts
table-name

The name of your existing database name.

--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

File name to import fields from. This options allows you to put all your fields in one json file, and then import it from the command line. This is a powerful feature which makes it easy for your to reused the same fields in multiple command now or later. More documentation about this feature can be found in the "assign model fields from a raw string" section.

--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.

--force

This option will override the field's file if one already exists.


Fields

Overview

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, we use the field name to come up with an English title.

If this option is left out, the label will be constructed based field's name.

validation

You can pass any valid Laravel validation rule. The rules should be separated by bar |.

For example: required|date|after:tomorrow

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

  • text
  • textarea
  • password
  • email
  • checkbox
  • radio
  • number
  • date
  • select
  • hidden
  • file
  • multipleSelect
  • selectRange
  • selectMonth

After the file is uploaded to the designated path, the filename is stored in the database. For everything to work properly, the data-type must be of some sort of a string type. Or modify the behavior of uploadFile method to handle the new file.

Note: when using checkbox, or multipleSelect, the items are stored in te database as json string there. Additionally, the items in the index or form views are displayed separated by the value of the provided delimiter

Note: when using file, a protected method called uploadFile will be added to the controller. By default this methods generates a new random filename and stores it in the path defined in config file codegenerator.files_upload_path.

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 it stores it using the power of the mutators function in Laravel.

When the data is presented on the show and index views, we display the options separated by the delimiter value. Of course, you can always change this behavior to fit your needs by removing the accessor and mutator methods in the model and modifying the views.

html-value

Default: null

A default value to set the field to.

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": "Please select your gender in English",
        "ar": "Please select your gender in Arabic",
        "fr": "Please select your gender in French"
    },
    "1": {
        "en": "Male in English",
        "ar": "Male in Arabic",
        "fr": "Male in French"
    },
    "2": {
        "en": "Female in English",
        "ar": "Female in Arabic",
        "fr": "Female in French"
    }
}
is-inline-options

Default = 0

If the html-type is set to radio or checkbox, setting this option to 1 will put the items next to each other instead of a vertical list.

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 = 1

Setting the value to 0 will prevent from adding this field to the index view.

is-on-form

Default = 1

Setting the value to 0 will prevent from adding this field to the form view.

is-on-show

Default = 1

Setting the value to 0 will prevent from adding this field to the show view.

is-on-views

Default = 1

Setting the value to 0 will prevent from adding this field to the index, form or show view. This is just a short way to change the visibility for all views.

Database Properties
data-type

Default = varchar

The database column type. The following are valid types. 'char', 'date', 'datetime', 'datetimetz', 'biginteger', 'bigint', 'blob', 'binary', 'bool', 'boolean', 'decimal', 'double', 'enum', 'list', 'float', 'int', 'integer', 'ipaddress', 'json', 'jsonb', 'longtext', 'macaddress', 'mediuminteger', 'mediumint', 'mediumtext', 'morphs', 'string', 'varchar', 'nvarchar', 'text', 'time', 'timetz', 'tinyinteger', 'tinyint', 'timestamp', 'timestamptz', 'unsignedbiginteger', 'unsignedbigint', 'unsignedInteger', 'unsignedint', 'unsignedmediuminteger', 'unsignedmediumint', 'unsignedsmallinteger', 'unsignedsmallint', 'unsignedtinyinteger', 'uuid', 'uuid'

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

Json file 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 = 0

Setting this value to 1 will make this column a primary with auto increment identity.

is-primary

Default = 0

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 = 0

Setting this value to 1 will add index to this column.

is-unique

Default = 0

Setting this value to 1 will add a unique index to this column.

is-nullable

Default = 0

Setting this value to 1 will make this column nullable.

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 = 0

Setting this value to 1 will make this column unsigned. This option should only be used with numeric types only.

comment

This option will allow you to add meta description of the field in the database.

Creating fields from JSON file

Storing the field's specification in a json file enables you to easily reuse the field in multiple commands. It also allows you to recreate the views in the future if you decided to add/remove fields after the views have been crested. 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.

Here is an example of post.json file

[{
    "name": "title",
    "data-type": "varchar",
    "html-type": "text",
    "validation": "required|min:1|max:20",
    "label": "Title without translations",
    "is-index": false,
    "is-on-index": true,
    "is-on-show": true,
    "is-on-form": true
}, {
    "name": "name",
    "data-type": "varchar",
    "html-type": "text",
    "validation": "required|min:1|max:20",
    "labels": {
        "en": "Name in English",
        "ar": "Name in Arabic",
        "fr": "Name in French"
    },
    "is-index": false,
    "is-on-views": true
}, {
    "name": "gender",
    "data-type": "int",
    "html-type": "select",
    "validation": "required|int",
    "labels": {
        "en": "gender in English",
        "ar": "gender in Arabic",
        "fr": "gender in French"
    },
    "options": {
        "": "Please select your gender",
        "1": "Male",
        "2": "Female"
    },
    "is-index": false,
    "is-on-views": true
}, {
    "name": "id",
    "is-auto-increment": true,
    "data-type": "bigint",
    "is-on-form": false
}]
    

Creating fields from a raw string

Of course, 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.