0

How to define relationship between below tables.

Class->belongsToMany('Student');

Student->belongsToMany('Class');

Class table and Student table pivoted by class_student table

I have Payment table as below

        $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 amount in the payment table means students can pay for each class tuition fee separately.

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.

So how can I define many to many relationship without pivot table for Payment table? Or Should I create pivot table for class_payment_student table?

Ryan Exlay
  • 361
  • 1
  • 3
  • 12
  • Define your relationships the other way around as well, with `->hasMany()` – silkfire Sep 07 '15 at 13:15
  • Why are you need a many to many relationship for a payment? One payment belongs to one user and to one class, am I right? - as your migration says. But "Payment has many students?" – Iamzozo Sep 07 '15 at 15:26

1 Answers1

0

You should create a pivot table payment_student. If you want to pass the pivot table name you can pass it as second argument as below

Example:

return $this->belongsToMany("your_table_name", "pivot_table_name");
fuesika
  • 3,280
  • 6
  • 26
  • 34