BelongsToMany

app/Models/Instance/Student/Student.php

use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    //
    public function calls()
    {
        return $this->belongsToMany(Call::class, 'student_call', 'student_id', 'call_id')
            ->withPivot('acc_id', 'contact_id')
            ->withTimestamps();
    }
    //
}

database/migrations/2022_12_09_145956_create_student_call_table.php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateStudentCallTable extends Migration
{
    public function up()
    {
        Schema::create('emotion_call', function (Blueprint $table) {
            $table->unsignedInteger('acc_id');
            $table->unsignedInteger('call_id');
            $table->unsignedInteger('student_id');
            $table->unsignedInteger('contact_id');
            $table->timestamps();
            $table->foreign('acc_id')->references('id')->on('acc')->onDelete('cascade');
            $table->foreign('call_id')->references('id')->on('calls')->onDelete('cascade');
            $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
            $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');
        });
    }
}

Leave a Reply