[扩展推荐] Laravel-5-Generators-Extended 代码生成器

说明

Generators 是一个 Laravel 代码生成器,允许你在执行一行命令的情况下快速生成代码文件,借鉴于 Ruby On Rails 的 rails generate 命令,见这里

完整的高质量扩展包推荐列表,请前往:下载量最高 100 个 Laravel 扩展包推荐

文章概览

  1. 安装;
  2. 代码示例;

安装

1). 使用 Composer 安装该扩展包:

composer require laracasts/generators --dev

2). 添加 Service Provider

一般情况下,你只需要在本地开发时才需要用到 generaters 扩展包,因此没必要去更新 config/app.php 文件中用于生产环境的 providers 数组. 更好的做法是,在 app/Providers/AppServiceProvider.php 中添加如下代码:

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

3). 运行 Artisan

这时候在命令行下运行

php artisan

可看到比之前多了些可操作的命令行:

至此安装成功。

代码实例

创建新的数据库表

运行以下命令行:

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

}

--schema="username:string, email:string:unique" 中携带的参数需要参照以下规范:

COLUMN_NAME:COLUMN_TYPE

可以清晰的看到 generators 自动帮我们生成了 users 表的 username 和 email 字段。

新增或删除数据库表字段

当命令行中出现 "remove" 或 "add" 关键词时,将会 "移除" 或 "新增" 数据库表字段。

以下是从 "posts" 表中删除 "user_id" 字段的实例

<?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"

每次当我们新建 migration 时,往往会接着创建相对应的 model,但使用 generators 创建新的 migration 时,将会默认帮我们创建好 model. 这就意味着,当我们运行以下命令时:

php artisan make:migration:schema create_dogs_table --schema="name:string"

将会得到 dogs 的 migration 和 model。如果不想要自动生成 model,可使用 --model=false 参数。

更多代码示例和说明可查看 Laravel-5-Generators-Extended

本项目由 The EST Group 成员 @monkey 整理发布,首发地为 Laravel China 社区,转载必须贴上原文链接 教程:【扩展推荐】Laravel-5-Generators-Extended 代码生成器

全文完。

本帖已被设为精华帖!
本帖由系统于 4年前 自动加精
monkey
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3

一直在使用

7年前 评论

想问下,这个默认的php artisan 不就有这些命令吗?

6年前 评论
jiangxiulong 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!