我嘗試在 Laravel 上添加模型關系。我知道我們如何做到這一點,但我不明白,我可以鏈接我需要的所有模型。
所以這是我的問題:
我建立了一個考試系統,它的作業原理是這樣的:
評估 -> 考試 -> QuestionCategory -> 問題 -> 回應。
所以在評估環節,我們選擇一個有問題類別、有問題、有回答的考試。
所以我寫了這個:
public function testtheorique($session){
$eval = Evaluation::find($session);
$caces = $eval->caces_cat;
$theorique = $eval->theorique;
$questioncat = $theorique->examcategory;
$question = $questioncat->categoryQuestions;
}
這是我的模型:
//Evaluation Model
public function theorique(){
return $this->belongsTo(TestTheorique::class);
}
//TestThorique Model
public function examcategory(){
return $this->hasMany(QuestionCategorie::class, 'exam_id');
}
//QuestionCategory Model
public function categoryQuestions(){
return $this->hasMany(Question::class, 'category_id', 'id');
}
//Question Model
public function qcategory(){
return $this->belongsTo(QuestionCategorie::class);
}
因此,當我制作此 dd($eval) 時,我將所有資料與關系一起顯示,但僅限于考試類別。我無法顯示問題,因為我沒有關系。
所以我不知道我怎么能有問題關系。我看不到我在哪里犯了錯誤。
謝謝你的幫助。
uj5u.com熱心網友回復:
因此,為了更精確,這正是我想要通過螢屏實作的。
所以這是我的完整功能:
public function testtheorique($session){
$eval = Evaluation::find($session);
$caces = $eval->caces_cat;
$theorique = $eval->theorique;
$questioncat = $theorique->examcategory;
$categories = QuestionCategorie::with(['categoryQuestions' => function ($query) {
$query->inRandomOrder()
->with(['reponse' => function ($query) {
$query->inRandomOrder();
}]);
}])
->whereHas('categoryQuestions')
->get();
return view('session.theorique', compact('categories', 'caces'))
->with('eval', $eval);
}
所以當我做一個 dd($eval) 我有這個:
有關系的評估模型
與 TestTheorique 的關系
然后與 QuestionCategorie 的關系
與 Question 無關的 QuestionCategorie 的詳細資訊
但我寫這篇文章是為了嘗試 $categories 并且我有關于 QuestionCategory 和 Response 關系的問題。這是 dd($categories) 的截圖
dd($categories) 的螢屏
QuestionCategorie 的詳細資訊
QuestionCategorie 中的關系細節
帶有回應關系的詳細問題
但這并不好,因為我有資料庫中出現的所有問題,而不是 TestTheorique 會話的一部分。
所以我想要一個評估會議。這節課有一個TestTheorique(Examid),然后,這個測驗有幾個QuestioCategorie,其中有幾個Question with Response。
uj5u.com熱心網友回復:
好的,解決了!所以這就是我做的。
在我的 TestTheorique 模型中,我將這一行更改為:
public function examcategory(){
return $this->hasMany(QuestionCategorie::class, 'exam_id')->with('getQuestion');
}
與 QuestionCategorie 建立關系并選擇 getQuestion 函式。在我的 QuestionCategorie 模型中,我添加了這個:
public function getQuestion(){
return $this->hasMany(Question::class, 'category_id', 'id')->with('reponse');
}
}
與問題建立關系并選擇回應功能。在我的問題模型中,我添加了這個:
public function reponse()
{
return $this->hasMany(QuestionReponse::class, 'question_id', 'id');
}
現在它可以作業了。如果我做了 dd($eval) 我會顯示所有會話資料,而不是資料庫中記錄的所有問題。
我真的要感謝Sumit Kumar的幫助,他給了我部分答案。
現在我繼續我的開發。
uj5u.com熱心網友回復:
對于僅使用 laravel/laravel 包,它只能讓您訪問具有 hasOneThrough 或 hasManyThrough 關系的 3 個表。
為了實作您想要的或至少我認為您所要求的可以在 staudenmeir/eloquent-has-many-deep 軟體包的幫助下完成。
對于 Laravel 8:
composer require staudenmeir/eloquent-has-many-deep:"^1.13"
對于 Laravel 9:
composer require staudenmeir/eloquent-has-many-deep:"^1.7"
然后,
在您的模型中:
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
并通過以下方式包含在您的模型類中:
use HasRelationships;
然后在您的模型方法中根據用法執行 hasOneDeep 或 hasManyDeep :
return $this->hasOneDeep('Model_Name_You_Want_To_Access_Through', ['Model_Access_Via1', 'Model_Access_Via2'], ['ForeignKeyOf_Model_Access_Via1', 'ForeignKeyOf_Model_Access_Via2', 'ForeignKeyOf_Model_Name_You_Want_To_Access_Through']);
這可以繼續進行,但請記住您必須根據結構提供陣列。
供參考:
https://github.com/staudenmeir/eloquent-has-many-deep
選項 2:
在您的 ExamId 模型中:您需要從 ExamId 模型創建一些關系來訪問問題,因此我們將使用 QuestionCategory 模型并通過它訪問所有內容。
ExamId 可能有多個 QuestionCategory (hasMany)
并且 QuestionCategory 會有多個問題 (hasMany)
所以在你的 ExamId 模型中:
public function getQCategory()
{
return $this->hasMany('App\Models\QuestionCategory')->with('getQuestion');
}
在您的問題類別模型中:
public function getQuestion()
{
return $this->hasMany('App\Models\Question');
}
在您的控制器中:
$exam = ExamId::with('getQCategory')->find($id);
現在$exam將包含 ExamId、Categories 和 Questions。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/520821.html
標籤:php拉拉维尔模型控制器
