在Laravel数据库迁移中使用特征

问题描述 投票:0回答:1

我的上司告诉我,我可以创建一个类似于seedertrait,然后可以在migration中使用它。在服务器上运行迁移时,迁移时数据库会自动获得种子,而不是在迁移成功后运行单独的种子服务器。

现在,我创建了一个特征,该特征包含在数据库迁移中。

<?php

namespace App\Services;

use App\Models\Module\Module;
use App\Models\Plan\Plan;

/**
 * PlanAndModuleGenerator class.
 */
trait PlanAndModuleGenerator
{
    private static $plans = [
        'free',
        'basic',
        'premium',
    ];

    public function up()
    {

        foreach ($this->plans as $planName) {

            // Get or create Plan.
            $plan = Plan::create([
                'machine_name' => '',
                'name' => $planName
            ]);
        }
    }
}

我的上司告诉我他们以前做过,但是我在互联网上找不到这样的东西。我包括了我的特质。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Services\PlanAndModuleGenerator;

class ModulePlan extends Migration
{
    use PlanAndModuleGenerator;
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('module_plan', function (Blueprint $table) {
            $table->unsignedInteger('plan_id');
            $table->unsignedInteger('module_id');

            $table->foreign('plan_id')->references('id')->on('plans');
            $table->foreign('module_id')->references('id')->on('modules');
        });
    }

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

[当我运行迁移时,up内部的trait功能未执行。我知道这是因为Plan表还没有种子。关于如何解决这个问题有什么想法吗?由于我的上司将在接下来的几天不在办公室,因此我无法访问他们以前在其中进行操作的存储库。

此外,有人可以告诉我如何正确调试此特征吗?我现在执行此操作的方式(仅运行迁移并等待错误)似乎有点麻烦。

laravel database-migration traits
1个回答
0
投票

它肯定不会起作用,因为您有两种up方法,一种在trait中,一种在migration中。您需要在迁移中删除up,并在特征中使用它,如下所示。这是特征

<?php

namespace App\Services;


use App\Models\Plan\Plan;
use App\Models\Module\Module;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;

/**
 * PlanAndModuleGenerator class.
 */
trait PlanAndModuleGenerator
{
    private static $plans = [
        'free',
        'basic',
        'premium',
    ];

    public function up()
    {
        $createmigration = Schema::create('module_plan', function (Blueprint $table) {
            $table->unsignedInteger('plan_id');
            $table->unsignedInteger('module_id');

            $table->foreign('plan_id')->references('id')->on('plans');
            $table->foreign('module_id')->references('id')->on('modules');
        });

        if ($createmigration) {
            foreach ($this->plans as $planName) {
                // Get or create Plan.
                $plan = Plan::create([
                    'machine_name' => '',
                    'name' => $planName
                ]);
            }
        }
    }
}

首先确认迁移是在创建计划之前创建的。

这是您的迁移的外观

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Services\PlanAndModuleGenerator;

class ModulePlan extends Migration
{
    use PlanAndModuleGenerator;
    
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('module_plan');
    }
}

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.