Romentigo
5 months ago
526
3
Laravel

How to set relations between 3 models?

Posted 5 months ago by Romentigo

I have 3 models: Department, Degree and Lecturer. They were made with migrations which I edited soon.

My migrations:

Departments

class CreateDepartmentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('departments', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->bigInteger('institute_id');
            $table->foreign('inst_id')
                ->references('id')
                ->on('institutes')
                ->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('departments');
    }
}

Degrees

class CreateDegreesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('degrees', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('degrees');
    }
}

Lecturers

class CreateLecturersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('lecturers', function (Blueprint $table) {
            $table->id();
            $table->string('last_name');
            $table->string('first_name');
            $table->string('middle_name');
            $table->string('phone_number');
            $table->string('email');
            $table->bigInteger('department_id');
            $table->foreign('department_id')
                ->references('id')
                ->on('departments')
                ->onDelete('cascade');
            $table->bigInteger('degree_id');
            $table->foreign('degree_id')
                ->references('id')
                ->on('degrees')
                ->onDelete('cascade');
            $table->unique(['last_name', 'first_name', 'middle_name']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('lecturers');
    }
}

As you can see, Lecturers has 2 foreign keys: to Degrees and Departments. How should I set up relations between them? I consider them to be smth like that, but I don't think it's correct.

Models:

Department

class Department extends Model
{
    public function institutes(){
        return $this->belongsTo('Institute');
    }

    public function lecturers(){
        return $this->hasMany('Lecturer');
    }
}

Degree

class Degree extends Model
{
    public function lecturers(){
        return $this->hasMany('Lecturer');
    }
}

Lecturer

class Lecturer extends Model
{
    public function departments(){
        return $this->belongsTo('Department');
    }

    public function degrees(){
        return $this->belongsTo('Degree');
    }
}

Please sign in or create an account to participate in this conversation.