在 Laravel 数据库迁移中使用特征
Use trait in Laravel database migration
我的上司告诉我,我可以创建一个类似 seeder
的 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');
}
}
当我 运行 迁移时,trait
中的 up
函数没有执行。我知道这一点是因为 Plan
table 尚未播种。关于如何解决这个问题的任何想法?由于我的上司接下来几天不在办公室,我无法访问他们之前这样做的存储库。
除此之外,谁能告诉我如何正确调试这个特性?我现在这样做的方式,只是 运行 迁移并等待错误,似乎有点麻烦。
它肯定不会工作,因为您有两种 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');
}
}
希望这会有所帮助
我完全看不出这个特征有任何理由,但如果你真的想使用它,你需要为特征的 up
方法起别名,然后在你的 up
迁移方法:
class ModulePlan extends Migration
{
use PlanAndModuleGenerator { up as traitUp; }
public function up()
{
...
$this->traitUp();
}
}
最好只为 Trait 中的方法使用不同的名称,但这个 trait 似乎没有理由首先出现。
我的上司告诉我,我可以创建一个类似 seeder
的 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');
}
}
当我 运行 迁移时,trait
中的 up
函数没有执行。我知道这一点是因为 Plan
table 尚未播种。关于如何解决这个问题的任何想法?由于我的上司接下来几天不在办公室,我无法访问他们之前这样做的存储库。
除此之外,谁能告诉我如何正确调试这个特性?我现在这样做的方式,只是 运行 迁移并等待错误,似乎有点麻烦。
它肯定不会工作,因为您有两种 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');
}
}
希望这会有所帮助
我完全看不出这个特征有任何理由,但如果你真的想使用它,你需要为特征的 up
方法起别名,然后在你的 up
迁移方法:
class ModulePlan extends Migration
{
use PlanAndModuleGenerator { up as traitUp; }
public function up()
{
...
$this->traitUp();
}
}
最好只为 Trait 中的方法使用不同的名称,但这个 trait 似乎没有理由首先出现。