简体   繁体   English

关联多个列时如何定义Laravel关系

[英]How to define Laravel relationship when multiple columns are associated

I have models just like I have mentioned below, How laravel eloquent-relationship can be implemented on it? 我有如下所述的模型,如何在其上实现laravel雄辩的关系?

User Model 用户模型

id
name

Post Model 邮政模型

id
title
desc
created_by = foreign key on User->id

Stage One (PostStageOneModel) 第一阶段(PostStageOneModel)

id
post_id
assigned_by
assigned_to

Stage Two (PostStageTwoModel) 第二阶段(PostStageTwoModel)

post_id
assigned_by  = foreign key on User->id
assigned_to  = foreign key on User->id

Stage Three (PostStageThreeModel) 第三阶段(PostStageThreeModel)

post_id
assigned_by  = foreign key on User->id
assigned_to  = foreign key on User->id

Stage Four (PostStageFourModel) 第四阶段(PostStageFourModel)

post_id
assigned_by  = foreign key on User->id
assigned_to  = foreign key on User->id

Stage Five (PostStageFiveModel) 第五阶段(PostStageFiveModel)

post_id
assigned_by  = foreign key on User->id
assigned_to  = foreign key on User->id

Stage Six (PostStageSixModel) 第六阶段(PostStageSixModel)

post_id
assigned_by  = foreign key on User->id
assigned_to  = foreign key on User->id

Relationships can specify which column they use: 关系可以指定它们使用的列:

public function assigner(){
    return $this->belongsTo(User::class, 'assigned_by');
}
public function assignee(){
    return $this->belongsTo(User::class, 'assigned_to');
}

User model should have an hasMany relationship type like below: 用户模型应具有hasMany关系类型,如下所示:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
    //...
    public function posts() {
        return $this->hasMany(Post::class);
    }
}

Posts belongs to Users via created_by attribute 帖子通过created_by属性属于用户

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
    //...
    public function createdBy() {
        return $this->belongsTo(User::class, 'created_by');
    }
}

PostStageOneModel belongs to posts and to users using 2 different attributes PostStageOneModel属于帖子和使用2个不同属性的用户

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PostStageOneModel extends Model {
    //...
    public function post() {
        return $this->belongsTo(Post::class);
    }
    public function assignedTo() {
        return $this->belongsTo(User::class, 'assigned_to');
    }
    public function assignedBy() {
        return $this->belongsTo(User::class, 'assigned_by');
    }
}

hope this help 希望这个帮助

I did it by defining following relations 我通过定义以下关系来做到这一点

In PostStageOneModel model 在PostStageOneModel模型中

public function assignedPost() {
    return $this->belongsTo(Post::class, 'post_id', 'post_id');
}

public function assigner()
{
    return $this->belongsTo(User::class, 'assigned_by', 'id');
}

public function assignee()
{
    return $this->belongsTo(User::class, 'assigned_to', 'id');
}

In Post Model 后期模型

public function identifiedBy() {
    return $this->belongsTo(User::class, 'identified_by', 'id');
}

public function postAssignedBy() {
    return $this->hasManyThrough(User::class, PostStageOneModel::class, 'post_id', 'id', 'post_id', 'assigned_by' );
}

public function postAssignedTo() {
    return $this->hasManyThrough(User::class, PostStageOneModel::class, 'post_id', 'id', 'post_id', 'assigned_to' );
}

In User Model 在用户模型中

public function assigned()
{
    return $this->hasMany(PostStageOneModel::class, 'assigned_by', 'id');
}

public function assignedTo()
{
    return $this->hasMany(PostStageOneModel::class, 'assigned_to', 'id');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM