amestsantim / generators
扩展 Laravel 5 的生成器(laracasts/generators 的分支)
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
Requires (Dev)
- phpspec/phpspec: ~2.1
README
重要:这是一个Laravel-5-Generators-Extended仓库的分支,分支的目的是合并一个长期未解决的拉取请求,该请求添加了一个可选的 --path 参数。我还编辑了迁移存根并移除了默认添加的 id 和时间戳字段。我添加了一个 --timestamps 开关来控制时间戳。我还添加了一个 --foreignReferenceName= 选项来传递引用表中 id 列的名称,如果需要与默认的 'id' 不同的话
如果你熟悉我的Laravel 4 生成器,那么这基本上是同一件事 - 只是升级到 Laravel 5。
L5 默认包含许多生成器,因此此包只需要添加一些东西,比如
make:migration:schema
make:migration:pivot
make:seed
还将添加一两个。
在 Laravel 5.5 上的使用
第 1 步:通过 Composer 安装
composer require amestsantim/generators
第 2 步:运行 Artisan!
你已经准备好了。从控制台运行 php artisan
,你将看到 make:*
命名空间部分的新的命令。
在 Laravel 5.4 和 5.3 上的使用
第 1 步:通过 Composer 安装
composer require amestsantim/generators
第 2 步:添加 Service Provider
你只想将生成器用于本地开发,因此你不想更新生产环境中的 providers
数组在 config/app.php
中。相反,将提供者在 app/Providers/AppServiceProvider.php
中添加,如下所示
public function register() { if ($this->app->environment() == 'local') { $this->app->register('Laracasts\Generators\GeneratorsServiceProvider'); } }
第 3 步:运行 Artisan!
你已经准备好了。从控制台运行 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" 关键字,这表示我们想要创建一个新表。
或者,我们可以使用“删除”或“添加”关键词,生成的样板代码将根据需要自动调整。让我们创建一个迁移来删除一个列。
php artisan make:migration:schema remove_user_id_from_posts_table --schema="user_id:integer"
现在,请注意我们正在使用正确的模式方法。
<?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"
现在,当你创建一个迁移时,通常你还想要一个模型,对吧?默认情况下,我们将自动为你创建一个与迁移对应的Eloquent模型。这意味着,如果你运行,比如
php artisan make:migration:schema create_dogs_table --schema="name:string"
你将得到一个包含模式的迁移...但你也会在app/Dog.php
中看到一个Eloquent模型。当然,你可以通过添加--model=0
标志/选项来选择退出。
如果你希望为迁移文件指定不同的路径,可以使用--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"
注意“外键”选项(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'); } }
注意,这里遵循了命名约定,不管你传递表名的顺序如何。
数据库播种器
php artisan make:seed posts
这个比较简单。它只是在“database/seeds”文件夹中给你一个快速播种器类。
<?php use Illuminate\Database\Seeder; // composer require laracasts/testdummy use Laracasts\TestDummy\Factory as TestDummy; class PostsTableSeeder extends Seeder { public function run() { // TestDummy::times(20)->create('App\Post'); } }