【问题标题】:Laravel 4: Eloquent and relationshipsLaravel 4:雄辩和关系
【发布时间】:2015-01-12 12:07:38
【问题描述】:

我在为数据透视表设置正确的 Eloquent 关系(belongsTo、hasMany、...)时遇到了麻烦。

为了清楚起见,我将缩写代码。 我有两个重要的表:“party”和“p2p_relations”。 这是各方

的迁移
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');
        });
    }

Party的模型

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 在类Relation

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

但是当我运行这个单元测试时,我得到一个错误:Undefined property: 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();

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

【问题讨论】:

  • 尝试查看文档。 laravel.com/docs/4.2/eloquent#relationships 不应要求您手动定义关系模型。
  • 因此将以下内容添加到派对模型中:public function context() { return $this->belongsToMany('Party', 'p2p_relations', 'context'); } public function reference() { return $this->belongsToMany('Party', 'p2p_relations', 'reference'); } 但是如何在关系上添加额外的属性?作为开始,种类,...
  • @jaan 首先阅读文档,检查withPivotwherePivot 方法。然后,从您的评论中修复关系。接下来看看这个:github.com/jarektkaczyk/Eloquent-triple-pivot

标签: php laravel-4 eloquent relationship


【解决方案1】:

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

更新了我的派对模型:

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

不需要关系模型。

数据透视表完美运行。

谢谢

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2020-09-25
    • 2018-07-24
    • 1970-01-01
    相关资源
    最近更新 更多