【发布时间】:2015-12-23 21:59:44
【问题描述】:
我正在为一款 RPG 游戏创建 API (Laravel 5),并尝试播种数据。
每个关卡包含多个任务,每个任务都属于一个关卡。
查看Laravel docs,我只看到为 one:one (associate) 和 many:many (sync, attach) 插入相关模型数据的关系,但对于 one:many (@ 987654324@).
您可以在下面的播种中看到我正在尝试将多个任务随机关联到关卡。我也试过attach 和sync 但得到错误
调用未定义的方法 Illuminate\Database\Query\Builder::associate()
和
调用未定义的方法 Illuminate\Database\Query\Builder::attach()
关卡模型:
public function quests()
{
return $this->hasMany(Quest::class);
}
任务模型:
public function levels()
{
return $this->belongsTo(Level::class);
}
级别播种机:
public function run()
{
Eloquent::unguard();
$levels = $this->createLevels();
$quests = Quest::all()->toArray();
/*
* Randomly join level id with associated quest ids
*/
foreach ($levels as $level) {
$level->quests()->associate($quests[rand(0, count($quests) - 1)]['id']);
}
【问题讨论】:
标签: php laravel laravel-5 seed