简体   繁体   English

Laravel雄辩的关系(一对一)

[英]Laravel eloquent relationship (one-to-one)

Can you please help me with this error, I am using laravel version 5.2.*. 您能帮我解决这个错误吗,我使用的是laravel 5.2。*版。 I have setup a One-to-One relation between a student and an application. 我已经在学生和应用程序之间建立了一对一的关系。 Please see the code below. 请参见下面的代码。

Student Model: 学生模型:

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

Application Model: 应用模式:

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

The database is correctly setup, I have a student_id FK in the Applications table. 数据库已正确设置,我在“应用程序”表中有一个student_id FK。 However, I am still getting the following error. 但是,我仍然收到以下错误。 What am I doing wrong, what did I miss? 我在做什么错,我想念什么? this worked before, it just broke all of a sudden. 在此之前,它突然就崩溃了。

BadMethodCallException in Builder.php line 2146: Call to undefined method Illuminate\\Database\\Query\\Builder::applications() Builder.php第2146行中的BadMethodCallException:调用未定义的方法Illuminate \\ Database \\ Query \\ Builder :: applications()

I get the error when I try to insert into the applications table, ie When trying to create an application for the student with the code below. 当我尝试插入应用程序表时,即尝试使用以下代码为学生创建应用程序时,出现错误。

$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

In Student model your function name is application not applications . Student模型中,您的功能名称是application而不是applications So change your code 所以改变你的代码

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

instead of 代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM