解析laravel hasManyThrough如何用?

下面由laravel教程欄目給大家介紹laravel hasmanythrough用法及參數,希望對大家有所幫助!

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

第一種情況,我稱之為傳導關聯表(簡單模式)

國家有很多用戶,用戶有很多帖子

countries     id - integer     name - string  users     id - integer     country_id - integer     name - string  posts     id - integer     user_id - integer     title - string

查詢某個國家的所有帖子,怎么實現?

countries為本表,posts為要輸出的目標表,users為中間表

return $this->hasManyThrough('AppPost', 'AppUser', 'country_id', 'user_id');

第二種情況,有中間表情況(純中間表)

exam_paper(試卷表)id nameexam_paper_question(試卷與試題中間表)id exam_paper_id question_idexam_question(試題表)id name

我們要通過exam_paper的id查詢question

return $this->hasManyThrough('exam_question', 'exam_paper_question', 'exam_paper_id', 'id','id','question_id');
//?參數1?目標表類名 exam_question, //?參數2?樞紐表類名 exam_paper_question, //?參數3?樞紐表中和當前表關聯的字段名 'exam_paper_question.exam_paper_id', //?參數4?目標表和樞紐表關聯的字段名 'exam_question.id', //?參數5?當前表中和樞紐表關聯的字段名 'exam_paper.id', //?參數6?樞紐表和目標表關聯的字段名 'exam_paper_question.question_id');

如果把當前表記作A,目標表記作B,中間表記作C,6個參數記作(B,C,CA,BC,AC,CB)

推薦學習:《laravel

以上就是解析

? 版權聲明
THE END
喜歡就支持一下吧
點贊5 分享