laracasts/generators

包含架构信息的 Laravel 高级生成器。

2.0.2 2023-03-22 14:23 UTC

This package is auto-updated.

Last update: 2024-09-22 17:54:10 UTC


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 团队维护,并由像你这样的社区成员添加功能和修复。所以,请随时参与进来。

https://user-images.githubusercontent.com/1032474/92732702-cd8b3700-f344-11ea-8e3b-ae86501d66fe.gif

目录

版本

根据你的 Laravel 版本,你应该:

安装

你可以使用 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"

注意“外键”选项(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');
	}

}

注意,这里遵循了命名惯例,无论你以什么顺序传递表名称。