通过 Laravel 5 Extended Generators 扩展包为 Laravel 5 数据库生成器锦上添花


0、开场

Laravel 5 已经为我们开箱提供了很多生成器,所以这个扩展包要做的只是在其基础上做一点锦上添花的事:

  • make:migration:schema
  • make:migration:pivot
  • make:seed
下面我们就来逐一揭开这三条命令的面纱,看看究竟给我们带来怎样的方便。

1、安装

开始之前,还是先安装扩展包,照例还是通过Composer来安装:

composer require laracasts/generators --dev

接下来注册服务提供者,由于我们只需要在本地开发环境使用这个扩展包,而如果注册到config/app.phpproviders数组,则会导致生产环境也会加载,所以我们只需在app/Providers/AppServiceProvider.php中注册它即可:

public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
    }
}

然后我们在项目根目录下运行php artisan,看看会发生什么,会新增几个make:*命令!

2、使用示例

安装完成后,就可以使用这个扩展包了,我们将逐一演示开场提到的三个命令的使用。

包含表结构的迁移

php artisan make:migration:schema create_users_table --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"

生成的迁移文件内容如下:

<?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模型类,如果你不想生成模型类,可以在上述命令后面加上--model=false标识。

外键约束

本扩展包提供的生成迁移命令也支持生成外键约束,假设你有一个文章表,每篇文章归属于某个用户:

php artisan make:migration:schema create_posts_table --schema="user_id:integer:foreign, title:string, body:text"

注意到这个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();
);

数据透视表

是否需要在数据库中设置一个数据透视表?这很简单,只需通过这样一个简单的命令就可以搞定整个类:

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');
    }
}

点赞 取消点赞 收藏 取消收藏

<< 上一篇: 在 Laravel 中使用 Laravel Searchy 扩展包实现基于数据库的轻量级搜索功能

>> 下一篇: 推荐:好用的 Laravel Repository 包