在laravel中定义关系问题

时间:2015-03-27 04:57:11

标签: php laravel laravel-migrations

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

Class->belongsToMany('Student');

Student->belongsToMany('Class');

由class_student表

旋转的类表和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表创建数据透视表吗?

1 个答案:

答案 0 :(得分:0)

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

示例:

return $this->belongsToMany("your_table_name", "pivot_table_name");