Laravel 3 Way Pivot当两列来自同一ID时



我该如何设置,我正在尝试构建学生和老师的课程时间表,这是我的代码。

 Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->text('photo_url')->nullable();
        $table->string('firstname');
        $table->string('lastname');
        $table->string('username')->unique()->nullable();
        $table->date('date_of_birth');
        $table->string('email')->unique();
        $table->string('password');
        $table->timeTz('timezone');
        $table->integer('is_active')->default(0);
        $table->tinyInteger('verified')->default(0);
        $table->string('email_token')->nullable();
        $table->text('address');
        $table->string('district');
        $table->rememberToken();
        $table->timestamps();
    });

,然后进行课程时间表

Schema::create('lessons', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('status'); //0 - not started, 1 - completed, 2 - no show
        $table->dateTime('start_at');
        $table->dateTime('end_at');
        $table->dateTime('started_at');
        $table->dateTime('ended_at');
        $table->string('unique_id')->unique();
 $table->integer('teacher_id')->unsigned();
          $table->foreign('teacher_id')
              ->references('id')->on('users')
              ->onUpdate('cascade')
              ->onDelete('cascade');
          $table->integer('student_id')->unsigned();
          $table->foreign('student_id')
              ->references('id')->on('users')
              ->onUpdate('cascade')
              ->onDelete('cascade');
        $table->integer('completed');
        $table->timestamps();
    });

如果我想在此视图上显示我的数据。如何将关系添加到各种模型以及如何显示它们以查看。

我相信这是属于属于的关系

请求帮助!

我该怎么做?

不确定我是否清除了足够的

我认为使用枢轴可能会限制您的某种限制,并使事情变得更复杂。

相反,为什么不创建诸如Schedule之类的模型?

Student has many Schedule
Schedule belongs to a Student
Schedule belongs to a Teacher
Schedule belongs to a Lesson
Student has many Lesson through Schedule

这也使您可以进一步扩展这一点,因为您是专门用于目的的模型。

最新更新