0

如何定义下表之间的关系。

Class->belongsToMany('Student');

Student->belongsToMany('Class');

由 class_student 表旋转的班级表和学生表

我有如下付款表

        $table->increments('id');
        $table->integer('class_id')->unsigned()->index();
        $table->integer('student_id')->unsigned()->index();
        $table->integer('payment_amount');
        $table->timestamps();
        $table->softDeletes();
        $table->foreign('class_id')->references('id')->on('classes')->onDelete('cascade');
        $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');

缴费表中的缴费金额是指学生可以单独支付每节课的学费。

Payment has many students. And Payment has many Class.

Class has many Payments. And Class has many students.

Student has many Classs. And Student has many payments.

那么如何在没有支付表的数据透视表的情况下定义多对多关系呢?或者我应该为 class_payment_student 表创建数据透视表吗?

4

1 回答 1

0

您应该创建一个数据透视表 payment_student。如果要传递数据透视表名称,可以将其作为第二个参数传递,如下所示

例子:

return $this->belongsToMany("your_table_name", "pivot_table_name");
于 2015-09-07T12:55:17.143 回答