【发布时间】:2015-12-23 21:42:33
【问题描述】:
我正在尝试在这些任务中播种任务和 NPC 的多对多连接表……一个任务可以有多个 NPC,一个 NPC 可以用于许多任务。我正在使用 Laravel 5。
在播种 Quest 表时,我也在播种连接表,但出现以下错误:
未找到基表或视图:1146 表 'clg_local.npc_quest' 不存在
创建 Quest 和 Quest_NPC 表:
public function up()
{
/*
* Create quests table
*/
Schema::create('quests', function(Blueprint $table)
{
$table->increments('id');
$table->string('quest_name')->nullable();
$table->unsignedInteger('reward_xp')->nullable();
$table->string('reward_items')->nullable();
$table->unsignedInteger('reward_money')->nullable();
});
/*
* Create quests_npcs join table
*/
Schema::create('quest_npc', function(Blueprint $table)
{
$table->increments('id');
$table->unsignedInteger('quest_id')->nullable();
$table->unsignedInteger('npc_id')->nullable();
});
在单独的创建中,我指定了我的关系:
Schema::table('quest_npc', function(Blueprint $table)
{
$table->foreign('quest_id')->references('id')->on('quests')->onDelete('cascade');
$table->foreign('npc_id')->references('id')->on('npcs')->onDelete('cascade');
});
显然我正在创建一个quest_npc 表,但它正在寻找一个npc_quest 表?
任务播种器:
public function run()
{
Eloquent::unguard();
$quests = $this->createQuests();
$npcs = Npc::all()->toArray();
foreach ($quests as $quest) {
$quest->npcs()->attach($npcs[rand(0, count($npcs) - 1)]['id']);
}
private function createQuests()
{
Quest::unguard();
$quests = [];
foreach (
[
[
'quest_name' => 'Quest 1',
'reward_xp' => 200,
'reward_items' => null,
'reward_money' => 200,
], ...
NPC模型:
public function npcs()
{
return $this->belongsToMany(Npc::class);
}
任务模型:
public function quests()
{
return $this->belongsToMany(Quest::class);
}
【问题讨论】:
标签: mysql database laravel laravel-5 seed