sprov03 / laravel-code-generator
此包已被弃用且不再维护。作者建议使用“不再维护”的包。
从数据库表中生成模板化代码。此包提供数据给您创建的模板,以便快速生成代码以构建项目框架,这样您就可以将更多时间用于有趣的工作。
5.8.9
2019-06-20 10:17 UTC
Requires
- php: >=5.0.0
- league/plates: ^3.3
This package is auto-updated.
Last update: 2019-09-26 01:45:20 UTC
README
使用php artisan命令,仅通过数据库表生成具有分页功能的完全工作的CRUD API端点。这附带一个配置文件,允许您引用项目模板和模板的动态目标路径,以便发布。
安装
composer require sprov03/laravel-code-generator --dev
自定义模板
此插件的最佳功能在于您可以根据自己的喜好制作自己的模板并生成代码。此步骤是使此功能正常工作所必需的。
运行此命令
php artisan vendor:publish
现在您将在 resources/templates/ 中找到需要修改的文件,以及一个新的配置文件 config/crud.php
使用方法
使用所需的表名作为输入
学生表的CRUD
// generate Model and Relationships for the students table
php artisan gen:relationships students
// generate Code Templates for a table/Model
php artisan gen:templates students
// generate Templates and Relationshps Command Combined into One
php artisan gen:full students
// Declare a Namespace For your Model
php artisan gen:full students --namespace=Models\\Users\\Students
// Select another custom Template Configuration other then default
php artisan gen:full students --template-set=api-endpoints
// Allows to overwrite template variables, or inject new ones
php artisan gen:full students --other-variables=action:Dancing|location:SomeLocation
// Will Overwrite Existing files, Mainly used when customizing your templates
php artisan gen:full students --force
// Will Display a full list of variables available to you in your templates
php artisan gen:full students --dump
您将需要回答一系列问题以确定需要生成的关联。在问题结束时,您的关联将被生成并放置在适当的类中。默认模板包括模型、模型测试、模型控制器、模型控制器测试、模型工厂和模型播种器。工厂、播种器和测试模板需要最后的润色,大约需要2分钟。模型验证规则已经给出,但需要根据您应用程序的详细信息进行调整。
数据库要求
/**
* No relationships defined on the user tabel
*/
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->index();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
/**
* Site belongs to user and user has many sites
*/
Schema::create('sites', function (Blueprint $table) {
$table->increments('id')->index();
/** A Foreign key is all that is required to setup belongsTo relationship and the reverse. */
$table->unsignedInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
/**
* Form belongs to user and user has many forms
*/
Schema::create('forms', function (Blueprint $table) {
$table->increments('id')->index();
/** A Foreign key is all that is required to setup belongsTo relationship and the reverse. */
$table->unsignedInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
/**
* Field Types commonly used for drop down selections to enforce data integrity
* Notice there is not id and the primary key is name
* This is primary key not being id is what triggers the required model template
* settings to get applied
*/
Schema::create('form_field_types', function (Blueprint $table) {
$table->string('name')->primary();
});
Schema::create('form_fields', function (Blueprint $table) {
$table->increments('id')->index();
$table->unsignedInteger('form_id')->index();
$table->foreign('form_id')->references('id')->on('forms')->onDelete('cascade');
$table->string('form_field_type')->index();
$table->foreign('form_field_type')->references('name')->on('form_field_types')->onDelete('cascade');
});
/**
* Many to many Two Foreign keys that are both part of a primary key will notify the script
* That this is a many to many relationship
*
* Note: To Not Create a model for the table use the following line
*
* php artisan gen:relationships form_site
*/
Schema::create('form_site', function (Blueprint $table) {
$table->unsignedInteger('site_id')->index();
$table->foreign('site_id')->references('id')->on('sites')->onDelete('cascade');
$table->unsignedInteger('form_id')->index();
$table->foreign('form_id')->references('id')->on('forms')->onDelete('cascade');
$table->primary(['site_id', 'form_id']);
});
/**
* Polymorphic MorphTo
* website type
* The Key to setting this up is the var_id and var_type on the same table
* This will set up the polymorphic relationships and build out the Interface
* Along with a handy Trait. All you have to do is implement the Interface and use the Trait
* On all the models that implement the interface and they will be ready to go.
*
* NOTE: the relationships being build will be the following.
* $site->website; website come form the website_id and website_type
* $website->site; site come form the model that is being referenced in this case will be the site
* You get to decide what the Interface is Called and All the necessary information gets filled out
*/
Schema::table('sites', function (Blueprint $table) {
$table->unsignedInteger('website_id')->index();
$table->string('website_type', '50')->index();
});
Schema::create('buying_websites', function (Blueprint $table) {
$table->increments('id')->index();
});
Schema::create('selling_websites', function (Blueprint $table) {
$table->increments('id')->index();
});
/**
* Polymorphic MorphToMany
* The same as above just this will be a morphMany in the interface instead of a morphOne
*/
Schema::create('histories', function (Blueprint $table) {
$table->increments('id')->index();
$table->unsignedInteger('subject_id')->index();
$table->string('subject_type')->index();
});
WalkThrew
/** Step 1 */ php artisan gen:full users /** Step 2 */ Open UserFactory.php and fill out the factory requirements. The Columns are setup so it will be easy Get your default data configured. /** Step 3 */ Open UserControllerTest and fill out default test data. The info is stubed out and will be qucik to setup each test to match what you will be sending form the fornt end. /** Step 4 */ Run the test The things that will fail is that all the columns are requied in the fillable array by default. This is by design, this is easy to modify and tayler to your application during this testing setup. /** Step 5 Only for Polymorphic Relationships */ Impliment the generated Interface and use the Trait by all models that need it.