medicivn/specified-stub-migration

v1.0.2 2022-08-10 08:52 UTC

This package is auto-updated.

Last update: 2024-09-10 13:06:25 UTC


README

创建指定占位符的迁移文件命令。参考 Laravel 的 "make:migration" 命令。

设置

\MediciVN\SpecifiedStubMigration\MigrateMakeCommand::class 添加到 app/Console/Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    protected $commands = [
        \MediciVN\SpecifiedStubMigration\MigrateMakeCommand::class,
    ];

MediciVN\SpecifiedStubMigration\MigrationServiceProvider::class 添加到 config/app.php

'providers' => [
    ...
    MediciVN\SpecifiedStubMigration\MigrationServiceProvider::class
],

选项

  • --table: 要迁移的表
  • --stubpath: 创建迁移文件占位符文件的路径
  • --path: 迁移文件应创建的位置
  • --realpath: 指示提供的迁移文件路径是预解析的绝对路径
  • --fullpath: 输出迁移文件的完整路径
  • --no-date-prefix: 文件名中不使用日期前缀

示例

php artisan make:specified_stub_migration create_users_table --table=users --stubpath=database/migrations/stubs/create_users_table.stub
Artisan::call("make:specified_stub_migration", [
    "name"          => "create_users_table",
    "--table"       => "users",
    "--stubpath"    => database_path("migrations/stubs/create_users_table.stub"),
]);

database/migrations/stubs/create_users_table.stub 的内容

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // The '{{ table }}' is placeholder where the value of `--table` parameter is set
        Schema::create('{{ table }}', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('phone');
            $table->string('dob');
            $table->string('whatever_you_want');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('{{ table }}');
    }
};