2016-02-04 91 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);