laravel belongsToMany 自引用附加关系

laravel belongsToMany self referencing attaching Relation

有点复杂和混乱... 但我有一个名为 building 的模型,它与同一模型有关系,但有 belongsToMany 关系...

在这个模型中我有以下关系:

# Building Require one or more another Building(s)
    public function requires() {
        return $this->belongsToMany('Building', 'building_require')->withPivot(array(
            'level',
            'updated_at',
            'created_at'
        ));
    }

现在我想填补我的中间 table building_require:

$obj = Building::find(1);
$obj->requires()->attach(4, array(
        'level' => 1,
        'created_at' => new DateTime,
        'updated_at' => new DateTime,
));

问题: 在我的 table 中,我现在找到这一行:

+-----+--------------+-------------+--------+----------------------+---------------------+
| id  | building_id  | require_id  | level  |     created_at       |     updated_at      |
+-----+--------------+-------------+--------+----------------------+---------------------+
|  1  |           4  |          0  |     1  | 2015-01-29 13:33:45  | 2015-01-29 13:33:45 |
+-----+--------------+-------------+--------+----------------------+---------------------+

但这是错误的..我希望这样:

+-----+--------------+-------------+--------+----------------------+---------------------+
| id  | building_id  | require_id  | level  |     created_at       |     updated_at      |
+-----+--------------+-------------+--------+----------------------+---------------------+
|  1  |           1  |          4  |     1  | 2015-01-29 13:33:45  | 2015-01-29 13:33:45 |
+-----+--------------+-------------+--------+----------------------+---------------------+

为什么会这样?

我可以自己回答。关系必须是:

# Building Require one or more another Building(s)
    public function requires() {
        return $this->belongsToMany('Building', 'building_require', 'building_id', 'require_id')->withPivot(array(
            'level',
            'updated_at',
            'created_at'
        ));
    }

也许有人会帮助它。