Laravel-多对一多态关系 [英] Laravel-Many-to-one Polymorphic relationship
问题描述
我正在使用laravel 5.1.场景如下(这是一个示例.实际场景与此示例相似)
I am using laravel 5.1. The scenario is as follows(this is an example. The real scenario is similar to this example)
我有3个模型
- 大学
- 学生
- 老师
一所大学可以有很多学生,但一个学生只能属于一所大学.
A college can have many students but a student can belong to only 1 college.
一所大学可以有很多老师,但是一个老师只能属于一所大学.
A college can have many teachers but a teacher can belong to only 1 college.
我想在laravel中建立这些表之间的关系. 一种方法是将 college_id 外键放在学生和教师"表上.但就我而言,此外键很多时候都为空.因此,与其在3-4个表中没有单独的列(大多数情况下为空值),我想探索为College表具有多态关系的选项.
I want to establish relationships between these tables in laravel. One of the methods for this is to place a college_id foreign key on the Students and Teachers table. But in my case, this foreign key will be null a lot of times. So rather than have separate columns in 3-4 tables with mostly null values, I wanted to explore the option of having a polymorphic relationship for College table.
这是我尝试过的: laravel docs(下面的链接)中给出的示例描述了一对多关系,而我的情况更多的是一对多关系.
This is what I tried: The example given in the laravel docs(link below) depict a one-to-many relationship whereas my scenario is more of a many to one relationship.
http://laravel.com/docs/5.1/eloquent-relationships#多态关系
如示例中所示,在College表上具有collegeable_id和collegeable_type列将无法满足我的要求,因为一所大学可以包含许多学生/老师,所以我创建了一个数据透视表:
As given in the example, having collegeable_id and collegeable_type columns on the College table would not have fulfilled my requirement as a college can contain many students/teachers so I created a pivot table:
Schema::create('collegeables', function (Blueprint $table) {
$table->integer('college_id')->unsigned();
$table->integer('collegeable_id')->unsigned();
$table->string('collegeable_type');
});
我有以下型号
大学模式:
namespace App;
use Illuminate\Database\Eloquent\Model;
class College extends Model
{
public function students()
{
return $this->morphedByMany('App\Student', 'collegeable');
}
}
学生模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
public function college()
{
return $this->morphOne('App\Colleges', 'collegeable');
}
}
通过这种安排,我能够使用像这样的College模型实例存储学生
With this arrangement, I am able to store students using College model instance like this
$college = \App\College::find(1);
$student = new \App\Student;
$student->name = 'John Doe';
$college->students()->save($student);
但是当我尝试使用下面指定的学生模型实例来检索College模型实例时,它给我一个错误:-
But when I try to retrieve a College model instance using a student model instance as specified below, it gives me an error:-
public function index()
{
return \App\Student::find(1)->college;
}
SQLSTATE [42S22]:找不到列:1054未知列'colleges.collegeable_id'
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'colleges.collegeable_id'
这是一种预期,因为morphOne可以处理表中的列. 如果我将学生模型中的morphOne函数更改为morphToMany,代码将开始工作,并且也能够检索值.但这使这种关系变得多对多,而这又又不是我想要的.
This is kind of expected as morphOne works with columns in a table I suppose. If I change the morphOne function in Student Model to morphToMany, the code starts working and I am able to retrieve values as well. But that make this relationship a many to many which again is not what I want.
所以我的问题是: 我可以在学生模型中使用它们的morphSomething函数,以便在保持一对多关系的同时检索学生所在大学的价值吗?
So my question is this:- Is their a morphSomething function I can use in the student model to be able to retrieve values for the student's college while maintaining the relationship as a one-to-many?
任何帮助将不胜感激.谢谢.
Any help would be really appreciated. Thanks.
推荐答案
这里没有理由使用多态关系.而是,只需将外键添加到students
和teachers
表上的colleges
表中.像这样:
There's no reason to use Polymorphic relationships here. Instead, just add a foreign key to your colleges
table on both your students
and teachers
tables. Like this:
colleges
id
name
teachers
id
name
college_id
students
id
name
college_id
然后您的模型可以使用belongsTo()
和hasMany()
关系,如下所示:
Then your models can use the belongsTo()
and hasMany()
relations, like so:
class College extends Model {
public function students() {
return $this->hasMany(App\Student::class);
}
public function teachers() {
return $this->hasMany(App\Teacher::class);
}
}
class Teacher extends Model {
public function colleges() {
return $this->belongsTo(App\College::class);
}
}
class Student extends Model {
public function colleges() {
return $this->belongsTo(App\College::class);
}
}
多态一对多关系与此关系相反,在这种关系中,您有一个只能与单个记录相关联的模型,但是该记录可以有许多不同的模型.
Polymorphic one-to-many relations are for the opposite of this relationship where you have a model that can only be related to a single record, but that record can be many different models.
为了进一步解释为什么这里不需要多态关系,让我们看一下它在哪里需要.假设您有一个简单的CRM风格的网站.有客户"和项目",并且您想对两者都有评论.在这种情况下,您将使注释成为多态关系,因为注释属于单个客户或单个项目,但不能同时属于两者.
To further explain why a polymorphic relationship isn't needed here, let's take a look at where it would be needed. Say you have a simple CRM style website. There are Customers and Projects and you want to have Comments on both. In this case, you would make Comments a polymorphic relationship because Comments belong to a single Customer or a single Project, but not both.
您的关系正好相反.在您的情况下,学生和教师属于一所大学.如果您遵循上一个示例的模式,则一所大学将属于一个学生或老师.
Your relationship is the exact opposite. In your case, Students and Teachers belong to a college. If you were to follow the previous example's pattern, a college would have belonged to a single student or teacher.
这篇关于Laravel-多对一多态关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!