orangehill / generators
扩展Laravel 5的生成器。
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
Requires (Dev)
- phpspec/phpspec: ~2.1
This package is auto-updated.
Last update: 2024-09-10 01:18:57 UTC
README
如果你熟悉我的 Laravel 4生成器,那么这基本上是同一件事 - 只是为了Laravel 5进行了升级。
L5自带了很多生成器,所以这个包只需要添加一些内容,比如
make:migration:schema
make:migration:pivot
make:seed
还有一些即将到来。
在Laravel 5.5上的使用
步骤1:通过Composer安装
composer require laracasts/generators --dev
步骤2:运行Artisan!
你已经设置好了。从控制台运行 php artisan
,你将在 make:*
命名空间部分看到新命令。
在Laravel 5.4和5.3上的使用
步骤1:通过Composer安装
composer require laracasts/generators --dev
步骤2:添加服务提供者
你只想将这些生成器用于本地开发,所以你不想更新生产环境中的 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”关键字开始,这表示我们想要创建一个新表。
或者,我们可以使用“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"
现在,当你创建迁移时,通常你希望有一个模型与之对应,对吧?默认情况下,我们将为你创建一个与迁移对应的Eloquent模型。这意味着,如果你运行,比如说
php artisan make:migration:schema create_dogs_table --schema="name:string"
你将得到一个带有模式的迁移...但你也会在 app/Dog.php
中得到一个Eloquent模型。当然,你可以通过添加 --model=0
标志/选项来选择退出。
使用 --filename 选项指定特定文件名
php artisan make:migration:schema create_doge_table --filename=2016_01_27_090200_create_doge_table.php
如果你想为迁移文件指定不同的路径,可以使用 --path
选项,如下所示
php artisan make:migration:schema create_dogs_table --path=\database\migrations\pets
如果你想从数据库中删除表,可以使用“drop”关键字。
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class DropOnionsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::drop('onions'); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::create('onions', function (Blueprint $table) { $table->increments('id'); $table->string('origin'); }); } }
别忘了提交你的列给 down 方法!还要注意,如果你尝试删除带有外键的表,你的迁移将在运行时失败!
外键约束
当你需要生成外键约束时,还有一些隐藏的甜点。想象一下,你有一个帖子表,其中每个帖子都属于一个用户。让我们试试
php artisan make:migration:schema create_posts_table --schema="user_id:integer:foreign, title:string, body:text"
注意“foreign”选项(user_id:integer:foreign
)?这是特殊的。它表示 user_id
应该收到一个外键约束。遵循约定,这将给我们
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users');
因此,对于完整的命令,我们的模式应该看起来像这样
Schema::create('posts', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('title');
$table->text('body');
$table->timestamps();
);
酷。
交叉表
所以你需要一个迁移来在你的数据库中设置一个交叉表?很简单。我们可以使用单个命令来生成整个类。
可用参数和选项
tableOne
- 第一个表名tableTwo
- 第二个表名--action
- 操作(创建、删除),默认:创建--columnOne
- 交叉表第一列的字段名(与第一个表相关)--columnTwo
- 交叉表第二列的字段名(与第二个表相关)--path
- 迁移文件路径--filename
- 迁移文件名--tableName
- 交叉表名称--className
- 迁移类名称--useForeignKeys
- 在交叉表上创建外键(默认:true)
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'); } }