簡體   English   中英

Laravel雄辯的關系(一對一)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM