I have models just like I have mentioned below, How laravel eloquent-relationship can be implemented on it?
User Model
id
name
Post Model
id
title
desc
created_by = foreign key on User->id
Stage One (PostStageOneModel)
id
post_id
assigned_by
assigned_to
Stage Two (PostStageTwoModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
Stage Three (PostStageThreeModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
Stage Four (PostStageFourModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
Stage Five (PostStageFiveModel)
post_id
assigned_by = foreign key on User->id
assigned_to = foreign key on User->id
Stage Six (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:
<?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
<?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
<?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
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');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.