joshcadman01 / laracasts-generators
高级Laravel生成器,包含模式信息。
Requires
- illuminate/support: ~6.0|~7.0|~8.0|~9.0|^10.0|^11.0
Requires (Dev)
- phpspec/phpspec: ~6.0
README
在make:migration
命令中轻松定义迁移模式。本包提供的新的命令有
make:migration:schema
make:migration:pivot
这允许您执行php artisan make:migration:schema create_dogs_table --schema="name:string:nullable, description:text, age:integer, email:string:unique"
并获得一个完整的迁移文件,您可以使用php artisan migrate
运行。对于这种简单的情况,无需在迁移文件中手动调整。如果您需要更改任何内容,这会更加容易,因为大部分代码已经生成。
由Jeffrey Way于2015年创建,作为其JeffreyWay/Laravel-4-Generators包的自然发展,为Laravel 5提供相同的功能。自2017年以来,由Backpack for Laravel团队维护,并由像您这样的社区成员添加功能和修复。因此,请随时贡献力量。
目录
版本
根据您的Laravel版本,您应该
- 使用JeffreyWay/Laravel-4-Generators用于Laravel 4;
- 使用本包的
v1
版本用于Laravel 5.0 - 5.8; - 使用本包的
v2
版本用于Laravel 6-8;
安装
您可以使用Composer安装本项目的v2版本,服务提供者将由Laravel自动加载
composer require --dev laracasts/generators
您已设置完毕。从控制台运行php artisan
,您将在make:*
命名空间部分看到新的命令。
示例
带模式的迁移
php artisan make:migration:schema create_users_table --schema="username:string, email:string:unique"
请注意我们用于声明任何适用模式的格式:用逗号分隔的列表...
COLUMN_NAME:COLUMN_TYPE
因此,以下任何一种都适用
username:string
body:text
age:integer
published_at:date
excerpt:text:nullable
email:string:unique:default('foo@example.com')
使用之前的模式...
--schema="username:string, email:string:unique"
...这将给出
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->string('username'); $table->string('email')->unique(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('users'); } }
在生成带有模式的迁移时,迁移名称(例如,“create_users_table”)很重要。我们使用它来确定您想完成什么。在这种情况下,我们以“create”关键字开始,表示我们想要创建一个新表。
或者,我们可以使用“remove”或“add”关键字,生成的样板代码将根据需要进行调整。让我们创建一个删除列的迁移。
php artisan make:migration:schema remove_user_id_from_posts_table --schema="user_id:integer"
现在,请注意我们正在使用正确的Schema方法。
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class RemoveUserIdFromPostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('posts', function(Blueprint $table) { $table->dropColumn('user_id'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('posts', function(Blueprint $table) { $table->integer('user_id'); }); } }
以下是一些您可能编写的其他命令示例
php artisan make:migration:schema create_posts_table
php artisan make:migration:schema create_posts_table --schema="title:string, body:text, excerpt:string:nullable"
php artisan make:migration:schema remove_excerpt_from_posts_table --schema="excerpt:string:nullable"
模型
现在,当您创建迁移时,通常还想要一个模型,对吧?默认情况下,本包不会为迁移创建模型。但它可以做到。只需指定--model=true
即可。
php artisan make:migration:schema create_dogs_table --schema="name:string" --model=true
迁移路径
如果您想指定迁移文件的不同路径,可以使用--path
选项,如下所示
php artisan make:migration:schema create_dogs_table --path=\database\migrations\pets
外键约束
还有生成外键约束的秘密糖。想象一下,您有一个文章表,其中每个文章属于一个用户。让我们试试
php artisan make:migration:schema create_posts_table --schema="user_id:unsignedInteger:foreign, title:string, body:text"
注意“foreign”选项(user_id:unsignedInteger:foreign
)。这是特殊的。它表示user_id
应该接受一个外键约束。遵循约定,这将给我们
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
因此,对于这个完整的命令,我们的模式应该看起来像这样
Schema::create('posts', function(Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('title');
$table->text('body');
$table->timestamps();
);
很酷。
交叉表
所以你需要迁移来在数据库中设置一个枢纽表?很简单。我们可以用单个命令来构建整个类。
php artisan make:migration:pivot tags posts
这里,我们可以按任何顺序传递两个表的名称,我们需要为这两个表创建一个连接/枢纽表。这将给你
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePostTagPivotTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('post_tag', function(Blueprint $table) { $table->integer('post_id')->unsigned()->index(); $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); $table->integer('tag_id')->unsigned()->index(); $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('post_tag'); } }
请注意,这里遵循了命名约定,无论你传递的表名顺序如何。