我的上司告诉我,我可以创建一个类似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
表尚未被植入。关于如何解决这个问题有什么想法吗?由于我的上司将在接下来的几天不在办公室,因此我无法访问他们以前在其中进行操作的存储库。
除此以外,谁能告诉我如何正确调试此特征?我现在执行此操作的方式(仅运行迁移并等待错误)似乎有点麻烦。