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

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

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

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表尚未被植入。关于如何解决这个问题有什么想法吗?由于我的上司将在接下来的几天不在办公室,因此我无法访问他们以前在其中进行操作的存储库。

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

nijiaoshenmene 回答:在Laravel数据库迁移中使用特征

我完全看不到该特征的任何原因,但是如果您真的想使用它,则需要对特征的up方法进行别名,然后在您的up中对其进行调用迁移方法:

class ModulePlan extends Migration
{
    use PlanAndModuleGenerator { up as traitUp; }

    public function up()
    {
        ...
        $this->traitUp();
    }
}

在Trait中为该方法使用一个不同的名称会更好,但是首先看起来没有这个特征的原因。

,

该方法肯定不会起作用,因为您有两种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');
    }
}

希望这会有所帮助

本文链接:https://www.f2er.com/3082245.html

大家都在问