2016-02-04 92 views
0

你能幫我解決這個錯誤嗎,我正在使用laravel 5.2版。*。我已經建立了學生和應用程序之間的一對一關係。請參閱下面的代碼。Laravel雄辯的關係(一對一)

學生型號:

public function application() { 
    return $this->hasOne('App\Application'); 
} 

應用模型:

public function student() { 
    return $this->belongsTo('App\Student'); 
} 

數據庫是正確的設置,我有一個student_id FK Applications表。但是,我仍然收到以下錯誤。我做錯了什麼,我錯過了什麼?這工作之前,它突然間突然崩潰了。

BadMethodCallException在Builder.php線2146:調用未定義 方法照亮\數據庫\查詢\生成器::應用程序()

我得到的錯誤,當我嘗試插入到應用程序表,即當試圖用下面的代碼爲學生創建一個應用程序時。

$student = new Student; 
//There's code to insert into the students table here 
$application = new Application([ 
      'academic_year_id' => $year, 
      'ref_no' => Application::getReferenceNumber($year)->Ref_no, 
      'certificate_id' => 1, //This will remain hard coded until the centre starts to offer more than one certificate 
      'status' => "PENDING", 
      'slug' => str_slug(Application::getReferenceNumber($year)->Ref_no . "" . $academic_year->year) 
     ]); 
     $student->applications()->save($application); //Insert into the application table 
+3

'$學生 - >應用程序() - >保存($應用程序);'將'$學生 - >應用() - >保存( $ application);'我認爲 –

+0

是的,謝謝..我怎麼錯過那個..我發誓我一遍又一遍地看着,爲什麼我不能看到S –

+1

@ Md.SahadatHossain Howdy,因爲你的評論是解決方案,你可以張貼它作爲答案,理查德接受它的目的是爲了結束問題並使社區受益。謝謝 – Theson

回答

0

Student模型的函數名applicationapplications。因此,更改代碼

$student->application()->save($application); 

,而不是

$student->applications()->save($application);