解析laravel hasManyThrough如何用?

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

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

第一種情況,我稱之為傳導(dǎo)關(guān)聯(lián)表(簡單模式)

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

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

查詢某個國家的所有帖子,怎么實(shí)現(xiàn)?

countries為本表,posts為要輸出的目標(biāo)表,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');
//?參數(shù)1?目標(biāo)表類名 exam_question, //?參數(shù)2?樞紐表類名 exam_paper_question, //?參數(shù)3?樞紐表中和當(dāng)前表關(guān)聯(lián)的字段名 'exam_paper_question.exam_paper_id', //?參數(shù)4?目標(biāo)表和樞紐表關(guān)聯(lián)的字段名 'exam_question.id', //?參數(shù)5?當(dāng)前表中和樞紐表關(guān)聯(lián)的字段名 'exam_paper.id', //?參數(shù)6?樞紐表和目標(biāo)表關(guān)聯(lián)的字段名 'exam_paper_question.question_id');

如果把當(dāng)前表記作A,目標(biāo)表記作B,中間表記作C,6個參數(shù)記作(B,C,CA,BC,AC,CB)

推薦學(xué)習(xí):《laravel

以上就是解析

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊5 分享