larav -多对一多态关系


Laravel-Many-to-one Polymorphic relationship

我使用的是laravel 5.1。场景如下(这是一个示例)。实际场景与本示例类似)

我有三个模型

    大学
  1. 老师

一个学院可以有很多学生,但是一个学生只能属于一个学院。

一个学院可以有很多老师,但是一个老师只能属于一个学院。

我想在laravel中建立这些表之间的关系。其中一个方法是在Students and Teachers表上放置一个college_id外键。但在我的例子中,这个外键很多时候都是空的。因此,与其在3-4个表中有单独的列,大部分为空值,我想探索为College表建立多态关系的选项。

这是我尝试过的:laravel文档中给出的例子描述了一对多的关系,而我的场景更像是多对一的关系。

http://laravel.com/docs/5.1/eloquent-relationships polymorphic-relations

如示例所示,在College表上拥有collegeable_id和collegeable_type列将无法满足我的要求,因为学院可以包含许多学生/教师,因此我创建了一个数据透视表:

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模型实例来存储学生,如下所示

$college = 'App'College::find(1);
$student = new 'App'Student;
$student->name = 'John Doe';
$college->students()->save($student);

但是当我尝试使用下面指定的学生模型实例检索College模型实例时,它给了我一个错误:-

public function index()
    {
        return 'App'Student::find(1)->college;
    }

SQLSTATE[42S22]: Column not found: 1054未知列'colleges.collegeable_id'

这是一种预期的morphOne工作与列在一个表我想。如果我将Student Model中的morphOne函数更改为morphmany,代码将开始工作,并且我也能够检索值。但这会让我们的关系变得多对多,这也不是我想要的。

我的问题是:-我是否可以在学生模型中使用morphSomething函数,以便能够检索学生所在学院的值,同时保持一对多的关系?

任何帮助都将非常感激。谢谢。

这里没有理由使用多态关系。相反,只需向studentsteachers表上的colleges表添加一个外键。这样的:

colleges
    id
    name
teachers
    id
    name
    college_id
students
    id
    name
    college_id

然后您的模型可以使用belongsTo()hasMany()关系,如下所示:

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);
    }
}
多态的一对多关系与这种关系相反,在这种关系中,你有一个只能与单个记录相关的模型,但该记录可以是许多不同的模型。编辑:为了进一步解释为什么这里不需要多态关系,让我们来看看哪里需要多态关系。假设你有一个简单的CRM风格的网站。这里有客户和项目,您希望对两者都有评论。在这种情况下,您将使Comments成为多态关系,因为Comments属于单个客户或单个项目,但不属于两者。

你们的关系正好相反。在你的情况下,学生和老师属于一个学院。如果你要遵循前面例子的模式,一个学院将属于一个学生或老师。

我也有类似的需求,并设法以另一种方式即兴发挥,利用morph的表结构。

你将需要一个Collegeable类Pivot (https://laravel.com/docs/9.x/eloquent-relationships#defining-custom-intermediate-table-models)

然后魔术:

public function college(){
    return $this->hasOneThrough(Colleges::class,
        Collegeable::class,
        'collegeable_id',
        'id',
        'id',
        'college_id',
    );
}

Collegeable Pivot类上:

namespace App'Models;
use Illuminate'Database'Eloquent'Relations'Pivot;
class Collegeable extends Pivot
{
    protected $table = 'collegeables';
}