跟进这个帖子Three-way many-to-many relationship in Laravel

我试图重现这个例子,因为我发现这是一种通过Eloquent实现三向多对多关系的简洁方法 .

但是当我尝试检索任何东西时,我收到错误:

$user->relations()->where('student_id', $student)->first()->plan();

Tinker告诉我应该有一个数据透视表relation_user . 如果是这种情况,解决方案赢得了显示迁移,有没有办法重现Carlos Herrera Plata在这个帖子上说的话?

EDIT

这是我实现它的链接上的示例

class Dftstudent extends Model
{
    function relations() {
        return $this->hasMany(Dftrelation::class);
    }
}
class Dftplan extends Model
{
    function relations() {
        return $this->hasMany(Dftrelation::class);
    }
}
class Dftuser extends Model
{
    function relations() {
        return $this->hasMany(Dftrelation::class);
    }
}

而在关系模型中

class Dftrelation extends Model
{
    function student() {
        return $this->belongsToMany(Dftstudent::class);
    }

    function user() {
        return $this->belongsToMany(Dftuser::class);
    }

    function plan() {
        return $this->belongsToMany(Dftplan::class);
    }
 //
}

关系表中的迁移:

Schema::create('dftrelations', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->integer('student_id')->unsigned();
    $table->integer('plan_id')->unsigned();
    $table->timestamps();
  });
  Schema::table('dftrelations', function(Blueprint $table) {
    $table->foreign('user_id')->references('id')->on('dftusers');
    $table->foreign('student_id')->references('id')->on('dftstudents');
    $table->foreign('plan_id')->references('id')->on('dftplans');
  });

而其他型号Dftstudent,Dftplan,Dftuser只包含id和名称 .

我给他们填写了以下数据:

INSERT INTO dftusers (name) VALUES ("admin1"), ("admin2");
INSERT INTO dftstudents (name) VALUES ("Pete"), ("Sam");
INSERT INTO dftplans (name) VALUES ("plan A"), ("plan B");
INSERT INTO dftrelations (user_id, student_id, plan_id) VALUES (1,1,1), (2,2,2);