2016-10-22 62 views
0

我允許用戶在添加學生時動態地添加多個科目,例如,當您添加學生時,您可以添加一個科目即Math,然後您將設置其屬性老師的姓名,信用卡等個小時,但保存文檔時,DB我需要將信息保存到我的中間表students_subjects在中間表中保存具有相同名稱的多個輸入Laravel

id | student_id | subject_id | teacher_name | credit_hours

user can add multiple fields like this

的問題是,我怎麼能保存每個患者的信息。我可以將subject_id保存在隱藏字段中,然後獲得該信息,但是如何知道此ID與該特定主題相關,並獲得了該ID的老師和學分。

回答

0

我怎麼可以保存信息爲每個主題

DB::table('students_subjects')->insert([ 
    'student_id' => $input['studentId'], 
    'subject_id' => $input['subjectId'], 
    'teacher_name' => $input['teacherName'], 
    'credit_hours' => $input['creditHours'], 
]); 

我可以保存在一個隱藏字段的subject_id和獲取,但我怎麼會知道這個ID是與特定主題

如果您有subject_id,那麼您就知道它與哪個主題相關。

得到老師和學分該ID

$result = DB::table('students_subjects') 
->select(['teacher_name', 'credit_hours']) 
->where('id', $id) 
->first(); 
0

所以你有3張桌子。和多對多的關係

  • 學生
  • 科目
  • student_subject

看看https://laravel.com/docs/5.2/eloquent-relationships#many-to-many瞭解定義你的雄辯模型。

從你的問題,我會考慮改變你的數據庫結構。

一名學生hasMany科目。一個科目屬於教師。

所以,我有4個表如下:

students 
    - id 
    - first_name 
    - last_name 
student_subject 
    - student_id 
    - subject_id 
    - credit_hours (assuming this is the number of hours the student has studied?) 
subjects 
    - id 
    - name 
    - credit_hours (assuming this is the total credit value) 
teachers 
    - id 
    - name 
    - subject_id 

如果老師可以教很多科目,那麼你就需要添加一個subject_teacher透視表。

添加學生時,如果您可以提供其值爲subject_id的主題的下拉列表,那麼當您選擇主題時 - 您將subject_id發回到php以準備插入數據庫。

+0

我的結構無法在此,我要求我的問題其實在這個問題問的解決方案。 –

+0

道歉,我誤解了你想要的,因爲這個問題寫得不好,有點神祕。我會嘗試添加另一個答案,但在此期間,請幫助我們通過重述問題的方式來幫助您,使其更容易理解。 – Gravy

相關問題