2017-08-30 34 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

相關問題