使用Laravel,处理最多包含四个外键的数据透视表的最佳方法是什么

使用Laravel,处理最多包含四个外键的数据透视表的最佳方法是什么,laravel,laravel-6,Laravel,Laravel 6,我目前使用的是多对多关系,其中一个表最多有四个外键 用户id|教育id|课程id|创建人 在哪里 user\u id和添加的参考了users表中的id education\u id参考了education表中的id course\u id参考课程表上的id 对于api,我想知道处理此问题以提高性能的最佳方法。到目前为止,我能够添加数据,但挑战是从相关表中检索数据 以下是模型 //User Class public function education(){ return $thi

我目前使用的是多对多关系,其中一个表最多有四个外键

用户id
|
教育id
|
课程id
|
创建人

在哪里

user\u id
添加的
参考了
users
表中的
id

education\u id
参考了
education
表中的
id

course\u id
参考课程表上的
id

对于
api
,我想知道处理此问题以提高性能的最佳方法。到目前为止,我能够添加数据,但挑战是从相关表中检索数据

以下是模型

//User Class
public function education(){
        return $this->belongsToMany(Education::class, 'education_user', 'user_id', 'education_id')
        ->withPivot('created_by', 'course_id', 'institution', 'obtained')
        ->withTimestamps();
    }

//Education Class
public function user(){
    return $this->belongsToMany(User::class, 'user_id')->withTimestamps();
}

public function courses(){
    return $this->belongsTo(Course::class, 'course_id');
}


//Course Class
public function education(){
    return $this->belongsTo(Education::class, )->withTimestamps();
}

public function user(){
    return $this->belongsTo(User::class, )->withTimestamps();
}
从我的控制器,我有

public function show($id)
{
    $user = User::findOrFail($id);
    return EducationUserResource::collection($user->education()->with(['courses'])->get());
}
返回以下数据

{
    "data": [
        {
            "data": {
                "education_id": 4,
                "name": "BSc",
                "last_updated": "7 hours ago",
                "course_id": 106,
                "course": '',//How can I get the name of the course where id on courses == 106 here
                "pivot": {
                    "user_id": 26,
                    "education_id": 4,
                    "created_by": 9,
                    "creator" : '', //How can I get the name of the user who created this resource here
                    "course_id": 106,
                    "institution": "Rivers State University",
                    "obtained": 2017,
                    "created_at": "2019-11-14 08:18:05",
                    "updated_at": "2019-11-14 08:18:05"
                }
            }
        }
    ]
}
这是我的资源

public function toArray($request)
    {
        return [
            'data' => [
                'education_id' => $this->id,
                'name' => $this->name,
                'last_updated'=> $this->whenPivotLoaded('education_user', function () {
                    return $this->pivot->updated_at->diffForHumans();
                    }),
                'course_id' => $this->whenPivotLoaded('education_user', function () {
                    return $this->pivot->course_id;
                    }),
                'course' => $this->whenLoaded('courses'),
                'pivot'=> $this->whenLoaded('pivot')

            ]
        ];
    }

谢谢大家。

不要将该表用作透视表,而是用作完整的实体表。您永远不应该有一个包含2个以上关系的“透视表”,它不再是透视表

通过为此表创建一个单独的模型,您将使查询更加容易,而不需要加载复杂的关系


在其他模型中,您可以使用关系(如),以确保不需要手动加载此新模型。

不要将该表用作透视表,而是用作完整的实体表。您永远不应该有一个包含2个以上关系的“透视表”,它不再是透视表

通过为此表创建一个单独的模型,您将使查询更加容易,而不需要加载复杂的关系


在其他模型中,您可以使用关系,例如,以确保不需要手动加载此新模型。

到目前为止,我能够添加数据,但挑战是从相关表中检索数据。
您能再解释一下吗?我能够将记录附加到该关系。我希望能够从
课程
表中查看课程,以及从
用户
中创建记录的人。到目前为止,我能够添加数据,但挑战是从相关表中检索数据。
您能再解释一下吗?我能够将记录附加到关系中。我希望能够从
课程
表中查看课程,以及从
用户
表中创建记录的人谢谢。。。我会尽快开始实施这个。。。我会给你一个反馈,谢谢你的回答,“你不应该使用超过两个关系的轴心”。它帮助了我。很高兴我能帮忙,考虑把这个标记为答案,其他用户也会发现这一点。谢谢…我会尽快开始实施这个。。。我会给你一个反馈,谢谢你的回答,“你不应该使用超过两个关系的轴心”。它帮助了我。很高兴我能帮上忙,考虑把这个标记为答案,其他用户也会发现这一点。