Laravel 4: Eloquent 和关系

Laravel 4: Eloquent and relationships

我在为枢轴 table.

设置正确的 Eloquent 关系(belongsTo、hasMany、...)时遇到问题

为了清楚起见,我将缩写代码。 我有两个重要的 table:'parties' 和 'p2p_relations'。 这是 parties

的迁移
public function up()
    {
        Schema::create('parties', function ($table) {
            $table->increments('id');
            $table->string('name');
            $table->unsignedInteger('kind');
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('kind')->references('id')->on('kinds');
        });
    }

这是 p2p_relations(党对党关系)

的迁移
public function up()
    {
        Schema::create('p2p_relations', function ($table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('context');
            $table->unsignedInteger('reference');
            $table->datetime('start');
            $table->datetime('end')->nullable();
            $table->unsignedInteger('kind')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('context')->references('id')->on('parties');
            $table->foreign('reference')->references('id')->on('parties');
            $table->foreign('kind')->references('id')->on('kinds');
        });
    }

派对

的模型
class Party extends Ardent
{
    use SoftDeletingTrait;

    protected $softDelete = true;
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];

    protected $table = 'parties';

    public static $rules = array(
        'name' => 'required',
        'kind' => 'required|numeric'
    );

}

关系模型

class Relation extends Ardent
{
    use SoftDeletingTrait;

    protected $softDelete = true;
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];

    protected $table = 'p2p_relations';

    public static $rules = array(
        'context' => 'required|numeric',
        'reference' => 'required|numeric',
        'kind' => 'required|numeric',
        'start' => 'required|date',
        'end' => 'date'
    );
}

如何设置关系以便将各方关联为关系中的上下文或参考。 我认为 belongsTo 会在 class Relation

中提供帮助
public function context() {
        return $this->belongsTo('Party', 'context', 'id');
    }
    public function reference() {
        return $this->belongsTo('Party', 'reference', 'id');
    }

但是当我 运行 这个单元测试时我得到一个错误: Undefined 属性: Relation::$context

$context = new Party();
        $context->name = 'Person A';
        $context->kind = 1;
        $context->save();

        $ref = new Party();
        $ref->name = 'Company B';
        $ref->kind = 2;
        $ref->save();

        $relation = new Relation();
        $relation->start = new DateTime();
        $relation->context()->associate($context);
        $relation->reference()->associate($ref);
        $relation->kind = 3;
        $relation->save();

有什么想法吗?我真的是这个框架的新手。

感谢提供的评论,我学到了很多:-)

更新了我的派对模型:

public function references() {
    return $this->belongsToMany('Party', 'p2p_relations', 'context', 'reference')
        ->withPivot('reference', 'start', 'kind')
        ->withTimestamps()   ;
}

不需要关系模型。

支点 table 完美运行。

谢谢