2017-08-30 25 views
0

我如何设置这个,我想建立一个学生和老师的课程安排,这里是我的代码。Laravel 3种方式当两列来自相同的编号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(); 
    }); 

,如果我想显示对这个观点我的数据。我如何将关系添加到各种模型,以及如何显示它们以查看。

我相信这是一个belongsToMany关系

普莱斯帮助!

我该怎么做?

不知道我是否足够清晰的

回答

0

我觉得用一个枢轴可以限制你几分,使事情变得更加复杂。

相反,为什么不创建一个模型,如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 

这也使您可以进一步扩展,因为您正在处理专门用于该目的的模型。

+0

所以最好有一个模型,为学生和老师没有角色? – Didi

+0

我猜你有一个用户可以是多种类型?这完全取决于。我想像学生和老师有不同的功能,所以给他们不同的用户可能会更容易。 – ollieread

相关问题