多列关联时如何定义Laravel关系
How to define Laravel relationship when multiple columns are associated
我有下面提到的模型,
laravel eloquent 关系如何实现呢?
用户模型
id
name
Post 型号
id
title
desc
created_by = foreign key on User->id
第一阶段(PostStageOneModel)
id
post_id
assigned_by
assigned_to
第二阶段(PostStageTwoModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
第三阶段(PostStageThreeModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
第四阶段(PostStageFourModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
第五阶段(PostStageFiveModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
第六阶段(PostStageSixModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
关系可以指定他们使用的列:
public function assigner(){
return $this->belongsTo(User::class, 'assigned_by');
}
public function assignee(){
return $this->belongsTo(User::class, 'assigned_to');
}
用户模型应具有如下 hasMany
关系类型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
//...
public function posts() {
return $this->hasMany(Post::class);
}
}
帖子通过 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 属于使用 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');
}
}
希望对您有所帮助
我是通过定义以下关系来做到的
在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');
}
在Post模型中
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' );
}
在用户模型中
public function assigned()
{
return $this->hasMany(PostStageOneModel::class, 'assigned_by', 'id');
}
public function assignedTo()
{
return $this->hasMany(PostStageOneModel::class, 'assigned_to', 'id');
}
我有下面提到的模型, laravel eloquent 关系如何实现呢?
用户模型
id
name
Post 型号
id
title
desc
created_by = foreign key on User->id
第一阶段(PostStageOneModel)
id
post_id
assigned_by
assigned_to
第二阶段(PostStageTwoModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
第三阶段(PostStageThreeModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
第四阶段(PostStageFourModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
第五阶段(PostStageFiveModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
第六阶段(PostStageSixModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
关系可以指定他们使用的列:
public function assigner(){
return $this->belongsTo(User::class, 'assigned_by');
}
public function assignee(){
return $this->belongsTo(User::class, 'assigned_to');
}
用户模型应具有如下 hasMany
关系类型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
//...
public function posts() {
return $this->hasMany(Post::class);
}
}
帖子通过 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 属于使用 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');
}
}
希望对您有所帮助
我是通过定义以下关系来做到的
在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');
}
在Post模型中
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' );
}
在用户模型中
public function assigned()
{
return $this->hasMany(PostStageOneModel::class, 'assigned_by', 'id');
}
public function assignedTo()
{
return $this->hasMany(PostStageOneModel::class, 'assigned_to', 'id');
}