[英]Laravel eloquent relationship (one-to-one)
您能帮我解决这个错误吗,我使用的是laravel 5.2。*版。 我已经在学生和应用程序之间建立了一对一的关系。 请参见下面的代码。
学生模型:
public function application() {
return $this->hasOne('App\Application');
}
应用模式:
public function student() {
return $this->belongsTo('App\Student');
}
数据库已正确设置,我在“应用程序”表中有一个student_id
FK。 但是,我仍然收到以下错误。 我在做什么错,我想念什么? 在此之前,它突然就崩溃了。
Builder.php第2146行中的BadMethodCallException:调用未定义的方法Illuminate \\ Database \\ Query \\ Builder :: applications()
当我尝试插入应用程序表时,即尝试使用以下代码为学生创建应用程序时,出现错误。
$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
在Student
模型中,您的功能名称是application
而不是applications
。 所以改变你的代码
$student->application()->save($application);
代替
$student->applications()->save($application);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.